MATLAB - 3D Animation plot - matlab

I have a matrix with 4 variables.
I want to plot the results as a 3D surface animation, having 3 of the variables as x,y and z and the 4th variable as time (t).
I imagine the code to be something like:
figure(1)
plot4d(Results(:,1), Results(:,2), Results(:,6), Results(:,3))
Here is some sample data:
X Y t Z
-0.1111 1.2670 1.1000 0.0000 0.0000 9.4568
-0.1111 1.2670 1.2000 0.0000 0.0000 15.9115
-0.1111 1.2670 1.3000 0.0000 0.0000 18.3639
-0.1111 1.2670 1.4000 0.0000 0.0000 22.3732
-0.1111 1.2670 1.5000 0.0000 0.0000 23.3274
-0.1111 1.2670 1.6000 0.0000 0.0000 23.3389
-0.1111 1.2670 1.7000 0.0000 0.0000 23.3437
-0.1111 1.2670 1.8000 0.0000 0.0000 22.0600
-0.1111 1.2670 1.9000 0.0000 0.0000 23.7531
-0.1111 1.2670 1.0000 0.0000 0.0000 0
-0.1111 1.2670 2.1000 0.0000 0.0000 0
-0.1111 1.2670 2.2000 0.0000 0.0000 0
-0.1111 1.2670 2.3000 0.0000 0.0000 0
-0.1111 1.2670 2.4000 0.0000 0.0000 0
-0.1111 1.2670 2.5000 0.0000 0.0000 0
-0.1111 1.2670 2.0000 0.0000 0.0000 23.1249
-0.1111 1.5330 1.1000 0.0000 0.0000 20.7103
-0.1111 1.5330 1.2000 0.0000 0.0000 40.2953
-0.1111 1.5330 1.3000 0.0000 0.0000 53.0801
-0.1111 1.5330 1.4000 0.0000 0.0000 63.1132
The goal is to produce a 3D surface plot that varies with time, so that it looks like someone is waving a blanket ;)

If what you plot is a line, then you could just display it frame by frame, pausing between them according to the time stamp (assumed to be in seconds):
x = Results(:,1);
y = Results(:,2);
z = Results(:,3);
t = Results(:,4);
dt = [diff(t);eps];
h = axes('Parent', figure());
hold(h, 'off');
for k = 1:numel(t)
plot3(h, x(1:k), y(1:k), z(1:k), '-*k');
pause(dt(k));
end;
If you want to capture the animation for the movie, check the MATLAB tutorial page.

Related

Why am I getting a matrix of zeros and infs when dividing matrices of the same dimension by element?

I'm trying to get two matrices to divide, properly, element by element.
Essentially, firstd is a 6x499 and secd is 6x498. I first eliminate firstd's extra elements by doing firstd(:,499)=[]; making it 6x498. Now the next step is to transform firstd into the nominator, nom=((firstd.^2)+1).^1.5; My denominator is just denom=secd;
Both nom and denom have come out as 6x498 matrices with real, non-zero data for each element. However, when doing Rlayer=nom./denom, Rlayer comes out as this ludicrous 6x498 zero-ridden matrix.
I also trimmed out the elements in denom that were =0 by changing them to 0.0001.
Segment of result for Rlayer (Columns 493 through 498)
-0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000
-0.0000 0.0000 -0.0000 0.0000 0.0000 -0.0000
-0.0000 0.0000 -0.0000 0.0000 0.0000 -0.0000
-0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000
-0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000
-0.0000 0.0000 -0.0000 0.0000 0.0000 -0.0000
Below are two segments of denom (Columns 487 through 492)
0.0250 0.0281 -0.0281 0.0125 -0.0500 0.0969
-0.0125 0.0750 -0.1219 0.1094 -0.0938 0.0937
0.0344 0.0406 -0.1094 0.1187 -0.1344 0.1531
0.0001 0.0250 0.0001 -0.0437 0.0500 0.0062
0.0781 -0.0219 0.0094 -0.0125 -0.0188 0.1062
0.0250 0.0438 -0.0812 0.0937 -0.1063 0.1562
(Columns 493 through 498)
-0.1187 0.1156 -0.0844 0.0688 -0.0406 0.0125
-0.0969 0.1094 -0.0906 0.0469 0.0062 -0.0156
-0.1375 0.1719 -0.1656 0.0781 0.0187 -0.0531
-0.0562 0.1188 -0.1500 0.1438 -0.1187 0.1187
-0.1781 0.2281 -0.2156 0.1750 -0.1250 0.0812
-0.1750 0.1938 -0.1469 0.0563 0.0031 -0.0156
and this is a segment of nom (Columns 493 through 498)
1.0904 1.0235 1.0881 1.0368 1.0769 1.0514
1.0685 1.0201 1.0769 1.0272 1.0497 1.0532
1.0928 1.0180 1.1210 1.0201 1.0568 1.0685
1.0568 1.0285 1.1001 1.0170 1.0952 1.0260
1.0952 1.0078 1.1380 1.0107 1.1026 1.0272
1.0928 1.0078 1.1077 1.0212 1.0463 1.0480
Why is this division leading to this result? I've tried dividing with rdivide, in a double for loop, and row by row in a for loop. All number types are double.

MatLab: Create 3D Histogram from sampled data

I have sampled data in the interval [0,1] in an Array transitions=zeros(101,101) which I want to plot as a 3D-histogram. transitions is filled with data similar to the example data provided at the end of this thread.
The first columns refers to the first observed variable X, the second column to the second variable Y and the third column is the normalized frequency. I.e. for the first row: the observed normalized frequency of the variable pair (0,0) is 0.9459. The sum of the normalized frequencies for (0,Y)thus is 1.
I tried to make (sort of) a 3D histogram with the following code:
x_c = (transitions(:,1) * 100)+1;
y = (transitions(:,2) * 100)+1;
z = transitions(:,4);
%A = zeros(10,10);
A = zeros(max(x_c),max(y));
for i = 1:length(x_c)
try
if(z(i)>0)
A(int32(x_c(i)), int32(y(i))) = abs(log(z(i)));
else
% deal with exceptions regarding log(0)
A(int32(x_c(i)), int32(y(i))) = 0;
end
catch
disp('');
end
end
bar3(A);
However, since it is sampled data in a discrete space A the output looks like the plot below. This is somehow misleading as there are 'gaps' in the plot (z-value = 0 for coordinates where I have no sampled data). I rather would like to have the sampled data being assigned to their corresponding plots, thus resulting in a 'real' 3d histogram.
By the way, as a result of my 'hack' of creating A also the x-,y- and z-scale is not correct. The 3D histogram's axes (all three) should be in the interval of [0,1].
ans =
0 0 0.9459
0 0.0500 0.0256
0 0.1000 0.0098
0 0.1100 0.0004
0 0.1500 0.0055
0 0.1600 0.0002
0 0.2000 0.0034
0 0.2100 0.0001
0 0.2500 0.0024
0 0.2600 0.0001
0 0.3000 0.0018
0 0.3200 0.0000
0 0.3700 0.0000
0 0.4000 0.0010
0 0.4200 0.0000
0 0.4500 0.0007
0 0.5000 0.0007
0 0.5300 0.0000
0 0.5500 0.0005
0 0.6000 0.0005
0 0.6300 0.0000
0 0.7000 0.0002
0 0.7400 0
0 0.7500 0.0003
0 0.7900 0.0000
0 0.8000 0.0002
0 0.8400 0.0000
0 0.8500 0.0002
0 0.8900 0.0000
0 0.9000 0.0002
0 0.9500 0.0001
0 1.0000 0.0001
0.0500 0 0.0235
0.0500 0.0500 0.0086
0.0500 0.1000 0.0045
. . .
. . .
. . .
. . .
. . .
0.9500 0.9000 0.0035
0.9500 0.9500 0.0066
0.9500 1.0000 0.0180
1.0000 0 0.0001
1.0000 0.0500 0.0001
1.0000 0.1000 0.0001
1.0000 0.1100 0.0000
1.0000 0.1500 0.0001
1.0000 0.1600 0.0000
1.0000 0.2000 0.0001
1.0000 0.2100 0.0000
1.0000 0.2500 0.0001
1.0000 0.2600 0.0000
1.0000 0.3000 0.0001
1.0000 0.3200 0.0000
1.0000 0.3700 0.0000
1.0000 0.4000 0.0002
1.0000 0.4200 0
1.0000 0.4500 0.0002
1.0000 0.5000 0.0003
1.0000 0.5300 0.0000
1.0000 0.5500 0.0004
1.0000 0.6000 0.0004
1.0000 0.6300 0.0000
1.0000 0.7000 0.0007
1.0000 0.7400 0.0000
1.0000 0.7500 0.0010
1.0000 0.7900 0.0000
1.0000 0.8000 0.0015
1.0000 0.8400 0.0001
1.0000 0.8500 0.0024
1.0000 0.8900 0.0002
1.0000 0.9000 0.0042
1.0000 0.9500 0.0111
1.0000 1.0000 0.3998
I found a solution by working on the non-aggregated data. In particular each row of the data set transitions contains one observation of Xand Y. I used the code below to produce a normalized 3D histogram (and a 2D map) as folllows:
function createHistogram(transitions)
uniqueValues = unique(transitions(:,1));
biases = cell(numel(uniqueValues),1);
for i = 1:numel(uniqueValues)
start = min(find(transitions(:,1) == uniqueValues(i)));
stop = max(find(transitions(:,1) == uniqueValues(i)));
biases(i) = mat2cell(transitions(start:stop,2));
end
combinedBiases = padcat(biases{1},biases{2},biases{3},biases{4},...
biases{5},biases{6},biases{7},biases{8},biases{9},biases{10},...
biases{11},biases{12},biases{13},biases{14},biases{15},biases{16},...
biases{17},biases{18},biases{19});
bins = 0:0.1:1;
[f, x] = hist(combinedBiases, bins);
%
% normalize
%
for i = 1:numel(f(1,:))
for j = 1:numel(f(:,i))
f(j,i) = f(j,i)/numel(biases{i});
end
end
bHandle = bar3(x, f);
ylim([-0.04,1.04])
for k = 1:length(bHandle)
zdata = get(bHandle(k),'ZData');
set(bHandle(k),'CData',zdata, 'FaceColor','interp');
end
colormap('autumn');
hcol = colorbar();
axis('square');
cpos=get(hcol,'Position');
cpos(4)=cpos(4)/3; % Halve the thickness
cpos(2)=0.4; % Move it down outside the plot#
cpos(1)=0.82;
set(hcol, 'Position',cpos);
xlabel('Enrollment biases');
ylabel('Aging biases');
zlabel('Bias transition probability');
title(strcat('Probability mass function of bias transitions (', device,')'));
set(gca,'XTick',0:2:20);
set(gca,'XTickLabel',0:0.1:1);
print('-dpng','-r600',strcat('tau_PMF3D_enrollment-ageing-', device));
view(2);
cpos(1)=0.84;
set(hcol, 'Position',cpos);
print('-dpng','-r600',strcat('tau_PMF2D_enrollment-ageing-', device));
end
From the comment on the question it appears you have the values you want to represent each bin count. If so an alternative solution is to plot using hist3 with "junk" data using correct x and y scales and then update the zdata of the surface object created with your bin data (modified to be in the correct format).
This modification to the bin data is fairly simple and consists of reshaping into a matrix then replicating and padding all the elements, the method is included in the code below.
Based on the ans variable at the end of the question, assuming
ans(:,1) gives x values
ans(:,2) gives y values
ans(:,3) gives the normalised bin counts
code
%// Inputs
zdata=ans(:,3); %// zdata=rand(21*21,1); % for testing
xvalues = 0:0.05:1;
yvalues = 0:0.05:1;
%// plot with junk data, [0,0] in this case
nx = numel(xvalues); ny = numel(yvalues);
bincenters = { xvalues , yvalues };
hist3([0,0],bincenters);
Hsurface = get(gca,'children');
%// apply bin count format
pad = [0 0 0 0 0;0 1 1 0 0;0 1 1 0 0;0 0 0 0 0;0 0 0 0 0]; %// padding for each point
ztrans=kron(reshape(zdata,[nx,ny]),pad); %// apply padding to each point
%// update plot
set(Hsurface,'ZData',ztrans)
%// to set colour based on bar height
colormap('autumn');
set(Hsurface,'CData',ztrans,'FaceColor','interp')
output

Adding a point in 3D graph

I have data as shown below and have no idea why the point (25,25,0) does not plotted into the graph.
X = [0,71,142,213,284,355,426;0,71,142,213,284,355,426;0,71,142,213,284,355,426;0,71,142,213,284,355,426;0,71,142,213,284,355,426;0,71,142,213,284,355,426];
Y = [0,0,0,0,0,0,0;71,71,71,71,71,71,71;142,142,142,142,142,142,142;213,213,213,213,213,213,213;284,284,284,284,284,284,284;355,355,355,355,355,355,355];
IntensityError =
1.0e-04 *
0.4609 0.0000 0.0000 0.0000 0.0000 0.0000 0.4609
0.9217 0.0000 0.0000 0.0000 0.0000 0.0000 0.9217
0 0.0000 0.0000 0.0000 0.0000 0.0000 0
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
mesh(X,Y,Error)
hold on plot3(25,25,0,'*')
When you run the above code, you will see that the point (25,25,0) is not plotted.
I have tried to solve this problem for many days. Please help me.
Not sure what you mean, but the point shows on the figure in my quick test:
X = [0,71,142,213,284,355,426;
0,71,142,213,284,355,426;
0,71,142,213,284,355,426;
0,71,142,213,284,355,426;
0,71,142,213,284,355,426;
0,71,142,213,284,355,426];
Y = [0,0,0,0,0,0,0;
71,71,71,71,71,71,71;
142,142,142,142,142,142,142;
213,213,213,213,213,213,213;
284,284,284,284,284,284,284;
355,355,355,355,355,355,355];
IntensityError = 1.0e-04 *[
0.4609 0.0000 0.0000 0.0000 0.0000 0.0000 0.4609;
0.9217 0.0000 0.0000 0.0000 0.0000 0.0000 0.9217;
0 0.0000 0.0000 0.0000 0.0000 0.0000 0;
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000;
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000;
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000];
figure;
mesh(X,Y,IntensityError), hold on;
plot3(25,25,0,'*');

Powers Table MATLAB

For this question, I'm supposed to create a NxN powers table in matlab using arrays.
The code I have so far is as follows:
C = [];
D = [];
N = input('Enter the value you would like to use for your NxN Powers Table: ');
for i = 1:N
for j = 1:N
C = [C;i^j];
end
C = transpose(C);
D = [D;C];
C = [];
end
D
This code works perfectly fine for any numbers from 1-9, as soon as I enter anything greater than that, it prints out weird values.
Here is the output I have using 5 as an input, and the second one is using 10 as an input.
Enter the value you would like to use for your NxN Powers Table: 5
D =
1 1 1 1 1
2 4 8 16 32
3 9 27 81 243
4 16 64 256 1024
5 25 125 625 3125
Enter the value you would like to use for your NxN Powers Table: 10
D =
1.0e+010 *
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0002 0.0010
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0002 0.0010 0.0060
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0006 0.0040 0.0282
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0002 0.0017 0.0134 0.1074
0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0005 0.0043 0.0387 0.3487
0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0010 0.0100 0.1000 1.0000
Any ideas what could be wrong with my code? Seems like a simple fix, I just can't figure out whats wrong with it. Any help is greatly appreciated. Thanks
Notice the 1.0e+010 *. It means that the numbers should be multiplied by 10000000000. Five digits are not enough to print it. Insert format long or format short g to see the whole numbers.
I think your code works fine. Note that 10^10 = 1e10; the very last element in your output D is indeed 1e10. Check individual elements D(i,j) to verify that those are correct. MATLAB can't display all the elements because some elements are so much larger than other ones; 1e10 has 10 digits in it, for instance, while 1^1 = 1 has 1 digit. So spacing would get screwed up if this behavior didn't happen.

Matrix creation MATLAB

I am building a nxn matrix in matlab with the following code:
x = linspace(a,b,n);
for i=1:n
for j=1:n
A(i,j) = x(j)^(i-1);
end
A
i
b(i) = (1/i)*x(n)^i - (1/i)*x(1)^i;
end
I am testing it with a=1 b=10 and n=10. I get the expected results up to i=8
i =
8
A =
Columns 1 through 7
1 1 1 1 1 1 1
1 2 3 4 5 6 7
1 4 9 16 25 36 49
1 8 27 64 125 216 343
1 16 81 256 625 1296 2401
1 32 243 1024 3125 7776 16807
1 64 729 4096 15625 46656 117649
1 128 2187 16384 78125 279936 823543
1 256 6561 65536 390625 1679616 5764801
Columns 8 through 10
1 1 1
8 9 10
64 81 100
512 729 1000
4096 6561 10000
32768 59049 100000
262144 531441 1000000
2097152 4782969 10000000
16777216 43046721 100000000
however from i=9 on it becomes this:
i =
9
A =
1.0e+09 *
Columns 1 through 9
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0003 0.0005
0.0000 0.0000 0.0000 0.0000 0.0001 0.0003 0.0008 0.0021 0.0048
0.0000 0.0000 0.0000 0.0001 0.0004 0.0017 0.0058 0.0168 0.0430
0.0000 0.0000 0.0000 0.0003 0.0020 0.0101 0.0404 0.1342 0.3874
Column 10
0.0000
0.0000
0.0000
0.0000
0.0000
0.0001
0.0010
0.0100
0.1000
1.0000
Can someone please tell me what is happening? I am not very experienced in matlab (I mostly use c++ or python) and so far can't seem to figure it out myself.
It's just a formatting issue for larger numbers. Try
sprintf('%20.0f', A(end,end))
and you will see that the number is correct. At least up to some point, where you will run into double representation problems...
Because a common scaling is applied to your data display. See in your output:
A =
1.0e+09 *
A common factor of 10^9 was factored out of every entry in your matrix.
You may want to adjust your output display using:
format short g