Can anyone help me make a 3d plot from these variables? - matlab

I am a kind of newbie in using Matlab and I have a problem of producing a 3d plot using these three variables.
The variables are:
P=[1 0.8 0.6 0.4 0.2];
N=[0.1429 0.2857 0.4286 0.5714 0.7143 0.8571 1.0000];
K =
0.0359 0.0340 0.0315 0.0298 0.0309
0.0700 0.0669 0.0618 0.0602 0.0601
0.1018 0.0961 0.0896 0.0866 0.0897
0.1270 0.1192 0.1152 0.1091 0.1127
0.1444 0.1390 0.1322 0.1235 0.1284
0.1556 0.1509 0.1424 0.1375 0.1419
0.1656 0.1598 0.1536 0.1466 0.1500
In Matlab, K is in the form of K(:,1),(K:2),k(:,3),K(:,4) and K(:,5)
can anyone help me please on how to do it?.
I have copied some sections of the code below:
P=[1 0.8 0.6 0.4 0.2];
LAMBDA = linspace(0,1,8) * c;
for iL = 1:length(LAMBDA)
lambda = LAMBDA(iL);
for iP=1:length(P)
[Pbi,Pbo,Pb,Rhoi,Rhoo,Rho]=Sim_traffic(lambdai(iL),lambdao(iL),mu,ci,co,1-epsi);
P_out_p(iL,iP) = (lambdai(iL).*(1-P_B_i_conv_s(iL)).*inner_outage(iL,iPR) + lambdao(iL).*(1-P_B_o_conv_s(iL)).*out_outage(iL,iPR));
end
end
N=LAMBDA./c;
K=P_out_p(:,:);

You're probably looking to use surf and will need meshgrid as well:
[p,n] = meshgrid(P,N)dimensions are wrong...
surf(p,n,K)
Although looking at the docs, I think you might be able to skip the meshgrid line here:
surf(P,N,K)

Related

difference between histcounts and imhist matlab

histcounts and imhist are not returning the same values for counts and bin locations.
x = [0.5000, 0.6429, 0.7143, 0.6429, 0.7857, 0.2857, 0.8571, 0.6429,0, 0.7857, 0.9286, 1.0000, 0.1429, 0.8571, 0.2857, 0.8571, 0.5714, 0.0714];
[c1, l1] = histcounts(x, 6)
c1 =
3 2 1 4 3 5
l1 =
0 0.1700 0.3400 0.5100 0.6800 0.8500 1.0200
[c2, l2] = imhist(x, 6)
c2 =
2 3 0 5 6 2
l2 =
0 0.2000 0.4000 0.6000 0.8000 1.0000
What could be the reason for that?
MATLAB
close all;clear all;clc
nbins=[6 16 26 46]
x = [0.5000, 0.6429, 0.7143, 0.6429, 0.7857, 0.2857, 0.8571, ...
0.6429,0, 0.7857, 0.9286, 1.0000, 0.1429, 0.8571, 0.2857, 0.8571, 0.5714, 0.0714];
one can take it from one side
for k=1:1:numel(nbins)
figure(k);
ax=gca;hold on;grid on
[C1, L1] = histcounts(x,nbins(k));
stem(L1(1:end-1),C1);hold on
[C2, L2] = imhist(x,nbins(k));
stem(ax,L2,C2)
end
or from the other, stem graphs not shown, quite similar to the above ones.
for k=1:1:numel(nbins)
figure(k);
ax=gca;hold on;grid on
[C1, L1] = histcounts(x,nbins(k));
stem(L1(2:end),C1);hold on
[C2, L2] = imhist(x,nbins(k));
stem(ax,L2,C2)
end
The point : imhist is a command for images and it applies an offset to all histogram bin locations depending upon the type of image fed in.
imhist doesn't have a cut-off for tiny images, so the sequence x is assumed as image, which it is not.
Read imhist details here.
In particular this table shows such offset

How to create a "skill-bias diagram" (meteorology)?

In my research area (meteorology) graphs within graphs are commonly produced.
more information about it can be found here.
Each of those lines joints up data points that have:
An x-value, between 0 and 1 (values greater than 1 should not be represented in the graph).
A y-value, between 0 and 1.
A PSS value, between 1 and -1.
A Frequency Bias value, ranging from 0 to +∞, but values higher than 4 are not displayed.
A False Alarm Ratio (FAR) value, ranging from 0.0 to 0.9. The False Alarm Ratio value is held constant at a particular value for each data point on any given line.
EDIT: To make things really concrete, I've drawn a pink dot on the graph. That dot represents a data point for which x=0.81, y=0.61, PSS=-0.2, B=3.05, FAR=0.8.
I am trying to reproduce something similar in MATLAB. Googling turned up a lot of answers like this, which feature inset figures rather than what I'm looking for.
I have the data organized in a 3D array, where each page refers to a different level of False Alarm Ratio. The page with a FAR of 0.8 (data here) starts out like this
Then there are other pages on the 3D array devoted to FARs of 0.7, 0.6, and so on.
Questions
1. Is it even possible to create such an graph in MATLAB?
2. If so, what function should I use, and what approach should I take? EDIT: I have working code (below) that creates a somewhat similar figure using the linear plot function, but the documentation for this function does not indicate any way to insert a graph inside another graph. I am not sure how helpful this code is, but have inserted it in response to the downvoter.
H = [0:0.01:1];
figure; hold on
fill([0 1 1],[0 0 1],[0 0.2 0.4]) % Deep blue
fill([0 1 0],[0 1 1],[0.4 0 0]) % Purple
low_colours = {[0 0.501 1],[0 0.8 0.4], [0.4 0.8 0], [0.8 0.8 0]};
high_colours = {[0.6 0 0],[0.8 0 0], [1 0.5019 0], [0.988 0.827 0.196]};
colour_counter = 0;
for ii = -0.8:0.2:0
colour_counter = colour_counter + 1;
if colour_counter < 5
colour_now = low_colours{colour_counter};
end
ORSS = ones(1,size(H,2))*ii;
F = (H .* (1-ORSS)) ./ ((1-2.*H) .* ORSS + 1);
plot(F,H)
fill(F,H,colour_now);
end
colour_counter = 0;
for ii = 0.8:-0.2:0
colour_counter = colour_counter + 1;
if colour_counter < 5
colour_now = high_colours{colour_counter};
end
ORSS = ones(1,size(H,2))*ii;
F = (H .* (1-ORSS)) ./ ((1-2.*H) .* ORSS + 1);
plot(F,H)
fill(F,H,colour_now);
end
I think I got what you want, but before you go to the code below, notice the following:
I didn't need any of your functions in the link (and I have no idea what they do).
I also don't really use the x and y columns in the data, they are redundant coordinates to the PSS and B.
I concat all the 'pages' in your data to one long table (FAR below) with 5 columns (FAR,x,y,PSS,FB).
If you take closer look at the data you see that some areas that supposed to be colored in the graph has no representation in it (i.e. no values). So in order to interpolate the color to there we need to add the corners:
FAR{end+1,:} = [0.8 0 0 0 4];
FAR{end+1,:} = [0.9 0 0 -0.66 3.33];
FAR{end+1,:} = [1 0 0 0 0];
FAR{end+1,:} = [1 0 0 -1 3];
Next, the process has 2 parts. First we make a matrix for each variable, that ordered in columns by the corresponding FAR value, so for instance, in the PSS matrix the first column is all PSS values where FAR is 0, the second column is all PSS values where FAR is 0.1, and so on. We make such matrices for FAR(F), PSS and FreqBias(B), and we initialize them with NaNs so we can have columns with different number of values:
F = nan(max(histcounts(FAR.FAR,10)),10);
PSS = F;
B = F;
c = 1;
f = unique(FAR.FAR).';
for k = f
valid = FAR.FAR==k & FAR.x<=1;
B(1:sum(valid),c) = FAR.FB(valid);
B(sum(valid):end,c) = B(sum(valid),c);
PSS(1:sum(valid),c) = FAR.PSS(valid);
PSS(sum(valid):end,c) = PSS(sum(valid),c);
F(:,c) = k;
c = c+1;
end
Then we set the colors for the colormap (which I partially took from you), and set the labels position:
colors = [0 0.2 0.4
0 0.501 1;
0 0.8 0.4;
0.4 0.8 0;
0.8 0.8 0;
0.988 0.827 0.196;
1 0.5019 0;
0.8 0 0;
0.6 0 0.2;
0.4 0.1 0.5];
label_pos =[0.89 0.77
1.01 0.74
1.14 0.69
1.37 0.64
1.7 0.57
2.03 0.41
2.65 0.18
2.925 -0.195
2.75 -0.55];
And we use contourf to plot everything together, and set all kind of properties to make it look good:
[C,h] = contourf(B,PSS,F);
xlim([0 4])
ylim([-1 1])
colormap(colors)
caxis([0 1])
xlabel('Frequency Bias B')
ylabel('Pierce Skill Score PSS')
title('False Alarm Ratio')
ax = h.Parent;
ax.XTick = 0:4;
ax.YTick = -1:0.5:1;
ax.FontSize = 20;
for k = 1:numel(f)-2
text(label_pos(k,1),label_pos(k,2),num2str(f(k+1)),...
'FontSize',12+k)
end
And here is the result:
Getting the labels position:
If you wonder what is a fast way to obtain the variable label_pos, then here is how I made it...
You run the code above without the last for loop. Then you run the following code:
clabel(C,'manual')
f = gcf;
label_pos = zeros(numel(f.Children.Children)-1,2);
for k = 1:2:size(label_pos,1)
label_pos(k,:) = f.Children.Children(k).Position(1:2);
end
label_pos(2:2:size(label_pos,1),:) = [];
After the first line the script will pause and you will see this message in the command window:
Carefully select contours for labeling.
When done, press RETURN while the Graph window is the active window.
Click on the figure where you want to have a label, and press Enter.
That's it! Now the variable label_pos has the positions of the labels, just as I used it above.

process a sequence of files loaded in Matlab

I have a series of files which I am loading in matlab and I am trying to do some interpolation like the following:
numfiles = 10;
data = cell(1, numfiles);
xvalues=[];
yvalues=[];
yqvalues=[];
xq=linspace(-10,10,1000);
for k = 1:numfiles
file = sprintf('filename_%d', k);
data{k} = importdata(file);
xvalues{k} = data{k}(:,1);
yvalues{k} = data{k}(:,2);
yqvalues{k}= interp1(xvalues{k},yvalues{k},xq,'spline');
end
Every thing works fine up to this point and I get the correct dimensions.
[1x1000 double] [1x1000 double] [1x1000 double] [1x1000 double] [1x1000 double] [1x1000 double] [1x1000 double] [1x1000 double] [1x1000 double] [1x1000 double]
Now I want to calculate the mean value at each xq. So I should have an array of 1*1000 values. How should I do that? Is it correct to use this loop
for i=1:length(xq)
m(i)=mean(yqvalues{k}(i));
end
after yqvalues{k}=... line inside the first loop?
I tried this but I get different values for mean in comparison to when I load the files separately one by one and interpolate each and the rest of the process.
Can some one help me with this issue please?!
Thanks
Example of the files:
file_1
-14.7812 0.25
-14.7712 0.36
-14.7612 0.20
-14.7512 0.14
-14.7412 0.05
-14.7312 0.05
-14.7212 0.1
-14.7112 0
-14.7012 0.25
-14.6912 0.23
file_2
-14.8289 0.34
-14.8189 0
-14.8089 0.1
-14.7989 0.08
-14.7889 0.15
-14.7789 0.22
-14.7689 0
-14.7589 0
-14.7489 0.28
-14.7389 0.36
file_3
-14.7813 0.05
-14.7713 0.25
-14.7613 0.17
-14.7513 0
-14.7413 0
-14.7313 0.09
-14.7213 0.02
-14.7113 0.18
-14.7013 0.30
-14.6913 0.04
With these files I am expecting mean values as:
D5_1 =
1.0e+09 *
Columns 1 through 13
-0.0000 -0.0002 -0.0022 -0.0082 -0.0203 -0.0408 -0.0718 -0.1156 -0.1743 -0.2501 -0.3453 -0.4619 -0.6022
Columns 14 through 20
-0.7684 -0.9627 -1.1872 -1.4441 -1.7357 -2.0641 -2.4315
But what I get is:
D_5 =
1.0e+04 *
Columns 1 through 13
-0.0381 -0.0184 -0.0070 -0.0016 -0.0000 0.0000 -0.0001 -0.0026 -0.0120 -0.0325 -0.0686 -0.1245 -0.2047
Columns 14 through 20
-0.3135 -0.4552 -0.6343 -0.8549 -1.1217 -1.4387 -1.8105
Your current implementation passes a single value (yqvalues{k}(i)) to mean, rather than the multiple values you are expecting to pass. I have made some changes to the code in order to utilize mean to find the average across each row of yqvalues:
numfiles = 3;
xvalues = cell(1, numfiles);
yvalues = cell(1, numfiles);
xq = linspace(-10,10,1000);
yqvalues = zeros(length(xq), numfiles);
for k = 1:numfiles
file = sprintf('file_%u.txt', k);
data = importdata(file);
xvalues{k} = data(:,1);
yvalues{k} = data(:,2);
yqvalues(:,k) = interp1(xvalues{k}, yvalues{k}, xq, 'spline');
end
m = mean(yqvalues, 2);
I'm going to assume that the number of XY values is variable in each file and keep xvalues and yvalues as cell arrays. However, since yqvalues is dependent on the size of xq, it is a constant and therefore we can use a simple double array for yqvalues and eliminate the need to put together a cellfun, loop, or other related approach to find the mean of each row.
As a general aside, I would urge caution with this interp1 approach, as it will likely return very poor results for xq values beyond the boundaries of your data.

MATLAB: How to calculate (on) Submatrices without a loop

I want to split a matrix columnwise into 3 segments and do a calculation on it (mean()). Is there a way to get this without a for-loop, as I did in this provided sample?
M = [2 4 9; 50 50 200; 30 0 0];
M = [M 10*M]
N = length(M);
seg = 3 % split in lets say 3 parts
segLen = round(N/seg)
segBeg = (((1:seg)-1) * segLen)+1 % start indices
segEnd = segBeg + segLen -1 % end indices
for i = 1: length(segBeg)
mean(M(:,segBeg(i):segEnd(i)),2)
end
Thank you!
Think outside the box: use the 3rd dimension:
r=reshape(M,size(M,1),segLen,[])
squeeze(mean(r,2))
The first line produces a 3d array with the first matrix at r(:,:,1), the second at r(:,:,2), ... (use M(:,1:seg*segLen) instread of M if the number of columns is not divisible by segLen).
mean(r,2) produces a nrows-by-1-by-seg array, squeeze makes a nrows-by-seg matrix out of it again.
You can use arrayfun together with cell2mat
result = cell2mat(arrayfun(#(x,y) mean(M(:,x:y),2), segBeg, segEnd,...
'UniformOutput', false))
This results in
result =
1.0e+03 *
0.0030 0.0145 0.0650
0.0500 0.3500 1.2500
0.0150 0.1500 0
where each column represents the mean across one submatrix.
Another solution using blockproc (like suggested by #DennisJaheruddin in the comments) could look like this
myFun = #(x) mean(x.data,2);
result2 = blockproc(M, [N, segLen], myFun)
This also results in
result2 =
1.0e+03 *
0.0030 0.0145 0.0650
0.0500 0.3500 1.2500
0.0150 0.1500 0
Note that blockproc can take advantage of parallel processing if the flag 'UseParallel' is set to true, i.e., result2 = blockproc(M, [N, segLen], myFun, 'UseParallel', true)
You can do for your example case
mean1 = mean(M(:,1:segLen))
mean2 = mean(M(:,segLen+1:N-segLen-1))
mean3 = mean(M(:,N-segLen:end))

Triangulation & Direct linear transform

Following Hartley/Zisserman's Multiview Geometery, Algorithm 12: The optimal triangulation method (p318), I got the corresponding image points xhat1 and xhat2 (step 10). In step 11, one needs to compute the 3D point Xhat. One such method is Direct Linear Transform (DLT), mentioned in 12.2 (p312) and 4.1 (p88).
The homogenous method (DLT), p312-313, states that it finds a solution as the unit singular vector corresponding to the smallest singular value of A, thus,
A = [xhat1(1) * P1(3,:)' - P1(1,:)' ;
xhat1(2) * P1(3,:)' - P1(2,:)' ;
xhat2(1) * P2(3,:)' - P2(1,:)' ;
xhat2(2) * P2(3,:)' - P2(2,:)' ];
[Ua Ea Va] = svd(A);
Xhat = Va(:,end);
plot3(Xhat(1),Xhat(2),Xhat(3), 'r.');
However, A is a 16x1 matrix, resulting in a Va that is 1x1.
What am I doing wrong (and a fix) in getting the 3D point?
For what its worth sample data:
xhat1 =
1.0e+009 *
4.9973
-0.2024
0.0027
xhat2 =
1.0e+011 *
2.0729
2.6624
0.0098
P1 =
699.6674 0 392.1170 0
0 701.6136 304.0275 0
0 0 1.0000 0
P2 =
1.0e+003 *
-0.7845 0.0508 -0.1592 1.8619
-0.1379 0.7338 0.1649 0.6825
-0.0006 0.0001 0.0008 0.0010
A = <- my computation
1.0e+011 *
-0.0000
0
0.0500
0
0
-0.0000
-0.0020
0
-1.3369
0.2563
1.5634
2.0729
-1.7170
0.3292
2.0079
2.6624
Update Working code for section xi in algorithm
% xi
A = [xhat1(1) * P1(3,:) - P1(1,:) ;
xhat1(2) * P1(3,:) - P1(2,:) ;
xhat2(1) * P2(3,:) - P2(1,:) ;
xhat2(2) * P2(3,:) - P2(2,:) ];
A(1,:) = A(1,:)/norm(A(1,:));
A(2,:) = A(2,:)/norm(A(2,:));
A(3,:) = A(3,:)/norm(A(3,:));
A(4,:) = A(4,:)/norm(A(4,:));
[Ua Ea Va] = svd(A);
X = Va(:,end);
X = X / X(4); % 3D Point
As is mentioned in the book (sec 12.2), pi T are the rows of P. Therefore, you don't need to transpose P1(k,:) (i.e. the right formulation is A = [xhat1(1) * P1(3,:) - P1(1,:) ; ...).
I hope that was just a typo.
Additionally, it is recommended to normalize each row of A with its L2 norm, i.e. for all i
A(i,:) = A(i,:)/norm(A(i,:));
And if you want to plot the triangulated 3D points, you have to normalize Xhat before plotting (its meaningless otherwise), i.e.
Xhat = Xhat/Xhat(4);
A(1,:) = A(1,:)/norm(A(1,:));
A(2,:) = A(2,:)/norm(A(2,:));
A(3,:) = A(3,:)/norm(A(3,:));
A(4,:) = A(4,:)/norm(A(4,:));
Could be simplified as A = normr(A).