MatLab: Create 3D Histogram from sampled data - matlab

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

Related

Matrix being divided by a value unexpectedly

Just working on multiply 2 rows of a matrix and making the answer another row within the same matrix. However when putting the new row in the matrix all of the values are for some reason divided by 1.0e+03.
X = (M(1,1:5) .* M(2,1:5));
M(3,1:5) = X(1,1:5)
disp(X)
Actual Result
X =
1.0e+03 *
0.4000 0.5500 0.7000 0.5000 0.6000
0.0030 0.0005 0.0008 0.0015 0.0050
1.2000 0.2750 0.5250 0.7500 3.0000
Expected Result
400.0000 550.0000 700.0000 500.0000 600.0000
3.0000 0.5000 0.7500 1.5000 5.0000
1200 275 525 750 3000

Solve System of Linear Equations in MatLab with Matrix of Arbitrary Size for Finite Difference Calculation

I am trying to write a script in MatLab R2016a that can solve a system of linear equations that can have different sizes depending on the values of p and Q.
I have the following equations that I am trying to solve, where h=[-p:1:p]*dx. Obviously, there is some index m where h=0, but that shouldn't be a problem.
I'm trying to write a function where I can input p and Q and build the matrix and then just solve it to get the coefficients. Is there a way to build a matrix using the variables p, Q, and h instead of using different integer values for each individual case?
I would use bsxfun(in recent matlab versions this function may be implented to the interpreter, I don't know for sure):
p = 4;
Q = 8;
dx = 1;
h = -p:p*dx
Qvector = [Q,1:Q-1]'
Matrix = bsxfun(#(Qvector, h)h.^(Qvector)./factorial(Qvector), Qvector, h)
Output:
h =
-4 -3 -2 -1 0 1 2 3 4
Qvector =
8
1
2
3
4
5
6
7
Matrix =
1.6254 0.1627 0.0063 0.0000 0 0.0000 0.0063 0.1627 1.6254
-4.0000 -3.0000 -2.0000 -1.0000 0 1.0000 2.0000 3.0000 4.0000
8.0000 4.5000 2.0000 0.5000 0 0.5000 2.0000 4.5000 8.0000
-10.6667 -4.5000 -1.3333 -0.1667 0 0.1667 1.3333 4.5000 10.6667
10.6667 3.3750 0.6667 0.0417 0 0.0417 0.6667 3.3750 10.6667
-8.5333 -2.0250 -0.2667 -0.0083 0 0.0083 0.2667 2.0250 8.5333
5.6889 1.0125 0.0889 0.0014 0 0.0014 0.0889 1.0125 5.6889
-3.2508 -0.4339 -0.0254 -0.0002 0 0.0002 0.0254 0.4339 3.2508

Summing cumulative area under curves of overapping triangles

I have two matrices for several triangles:
x =
2.0000 5.0000 10.0000
8.0000 10.0000 12.0000
12.0000 24.0000 26.0000
22.0000 25.0000 28.0000
23.0000 26.0000 25.0000
23.5000 27.0000 27.5000
20.0000 23.0000 27.0000
21.0000 24.0000 27.0000
24.0000 25.0000 27.0000
24.0000 26.0000 27.0000
24.0000 28.0000 29.0000
19.0000 22.0000 25.0000
18.0000 21.0000 23.0000
y =
0 1.0000 0
0 0.8000 0
0 0.6000 0
0 0.8000 0
0 0.8000 0
0 0.8000 0
0 1.0000 0
0 1.0000 0
0 1.0000 0
0 1.0000 0
0 1.0000 0
0 1.0000 0
0 1.0000 0
one line is one triangle. Columns are x and y positions of each point of the triangles.
So, I plot all these triangles and I need to sum the cumulative area under the curve of the triangles.
I try to use the area function, but I couldn't find how to sum their areas.
EDIT: I need to plot the sum of the areas on a red line in the same graphics. So I don't want a number like 20 cm²... I would like something like that:
I suggest that you interpolate to create all your individual triangles and then add the results. First you will need to augment your x and y matrices with the beginning (the origin) and end points like so:
m = 30; %// This is your max point, maybe set it using max(x(:))?
X = [zeros(size(x,1),1), x, ones(size(x,1),1)*m];
Y = [zeros(size(y,1),1), y, zeros(size(y,1),1)];
then perform all the interpolations (I'll sum as I go):
xi = 0:0.1:m;
A = zeros(1,size(xi,2)); %// initialization
for row = 1:size(x,1)
A = A + interp1(X(row,:), Y(row,:), xi);
end
and finally plot:
plot(x,y,'k')
hold on
plot(xi,A,'r','linewidth',2)
using your example data this gives:

Plot a disc in Matlab, color varying with angle

I would like to represent some data depending on the angle of measure. I have something like that:
angle=[0 90 180 270 360];
value=[1 2 3 4];
In my case, length(angle)=73
How can I plot a disc in which the color of the radius line varies according to the vector value.
I would like something like imagesc but in a disc.
I unsuccessfully tried using rose, but it is limited to a max of 20 angles.
This is another alternative that does not use 3D graphics, only 2D. It uses the primitive patch command that draws triangles. Here is a result:
function PlotDisc
thetas=(0:5:360)'*pi/180;
% r=(0:0.1:1)';
r = 1;
[x,y]=pol2cart(thetas,r);
colMap = GetColMap();
for i=1:numel(x)-1
X = [0 x(i) x(i+1) 0];
Y = [0 y(i) y(i+1) 0];
col = i / (numel(x)-1) * numel(colMap);
patch(X,Y,col,'EdgeAlpha',0);
end
axis equal
end
function colMap = GetColMap()
colMap = ...
[0.0417 0 0
0.0833 0 0
0.1250 0 0
0.1667 0 0
0.2083 0 0
0.2500 0 0
0.2917 0 0
0.3333 0 0
0.3750 0 0
0.4167 0 0
0.4583 0 0
0.5000 0 0
0.5417 0 0
0.5833 0 0
0.6250 0 0
0.6667 0 0
0.7083 0 0
0.7500 0 0
0.7917 0 0
0.8333 0 0
0.8750 0 0
0.9167 0 0
0.9583 0 0
1.0000 0 0
1.0000 0.0417 0
1.0000 0.0833 0
1.0000 0.1250 0
1.0000 0.1667 0
1.0000 0.2083 0
1.0000 0.2500 0
1.0000 0.2917 0
1.0000 0.3333 0
1.0000 0.3750 0
1.0000 0.4167 0
1.0000 0.4583 0
1.0000 0.5000 0
1.0000 0.5417 0
1.0000 0.5833 0
1.0000 0.6250 0
1.0000 0.6667 0
1.0000 0.7083 0
1.0000 0.7500 0
1.0000 0.7917 0
1.0000 0.8333 0
1.0000 0.8750 0
1.0000 0.9167 0
1.0000 0.9583 0
1.0000 1.0000 0
1.0000 1.0000 0.0625
1.0000 1.0000 0.1250
1.0000 1.0000 0.1875
1.0000 1.0000 0.2500
1.0000 1.0000 0.3125
1.0000 1.0000 0.3750
1.0000 1.0000 0.4375
1.0000 1.0000 0.5000
1.0000 1.0000 0.5625
1.0000 1.0000 0.6250
1.0000 1.0000 0.6875
1.0000 1.0000 0.7500
1.0000 1.0000 0.8125
1.0000 1.0000 0.8750
1.0000 1.0000 0.9375
1.0000 1.0000 1.0000] ;
end
Some time ago I had to plot polar data and looked for something similar. I do not know if it corresponds to your needs but I adapted my solution to your problem, here it is:
theta=(0:360)'*pi/180;
r=(0:0.1:1)';
value=repmat([360 (1:360)],size(r,1),1);
[THETA,R]=meshgrid(theta,r);
[X,Y]=pol2cart(THETA,R);
surf(X,Y,value,'edgecolor','none');
view(0,90);
UPDATE: You are absolutely right Andrey, and I suspect the 3D graphics to take more memory and thus display a lot slower but at the time I did not find any ohter way. Your solution is better, I will use it from now. The only thing I would change is the for loop:
r=1;
dtheta=0.01;
theta=linspace(0,2*pi,100)';
n=numel(theta);
[x,y]=pol2cart(theta,r);
figure;
cmap=colormap(jet);
X=[zeros(1,n-1);x(1:end-1)';x(2:end)';zeros(1,n-1)];
Y=[zeros(1,n-1);y(1:end-1)';y(2:end)';zeros(1,n-1)];
C=numel(cmap)*(1:n-1)/n-1;
patch(X,Y,C,'EdgeAlpha',0);
axis equal;

Syntax error for rectangular image

There is an image X of N*M size row M,column N. There are other 2 images A,B of same size as X. The objective is to shuffle the rows of X with the rows extracted from A and shuffle the columns of X with the columns extracted from B resulting in a totally modified img.
I am stuck at the point where simultaneously this is occuring. I am aware about the colon operator with which the code runs but for a square image. Please help how to go about it.
X=imread('picture.jpg');
[r c]=size(X);
[dummy,rowscrambleIdx]=sort(A,1);
X_shuffled=X;
[dummy,colscrambleIdx]=sort(B,2);
EDIT: The following code works for square image and I want to do similar operation for a rectangular sized image. However, this code does not work for rectangular sized image. I have tried to make the first code follow a similar logic but it does not work for a non-square RGB image having say 256*240*3 size
X=imread('picture.jpg');
[dummy,scrambleIdx]=sort(A(:));
X_shuffled=X;
X_shuffled(:)=A(scrambleIdx);
%now unscrambling
[dummy,unscrambleIdx] = sort(scrambleIdx);
X_recovered=X;
X_recovered(:)=X_shuffled(unscrambleIdx);
Why not use randi to randomly create some number of indicies to pull from A, and some to pull from B?
Example:
m=10;
n=5;
A=rand(m,n)
B=ones(m,n)
%3x1 vector of random ints b/w 1 and 10
index=randi([1 10],3,1);
rand_row = A(index,:);
B(index,:)=rand_row
Then output is:
index =
10
9
4
A =
0.9797 0.1174 0.7303 0.6241 0.2619
0.4389 0.2967 0.4886 0.6791 0.3354
0.1111 0.3188 0.5785 0.3955 0.6797
0.2581 0.4242 0.2373 0.3674 0.1366
0.4087 0.5079 0.4588 0.9880 0.7212
0.5949 0.0855 0.9631 0.0377 0.1068
0.2622 0.2625 0.5468 0.8852 0.6538
0.6028 0.8010 0.5211 0.9133 0.4942
0.7112 0.0292 0.2316 0.7962 0.7791
0.2217 0.9289 0.4889 0.0987 0.7150
B =
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
B =
1.0000 1.0000 1.0000 1.0000 1.0000
1.0000 1.0000 1.0000 1.0000 1.0000
1.0000 1.0000 1.0000 1.0000 1.0000
0.2581 0.4242 0.2373 0.3674 0.1366
1.0000 1.0000 1.0000 1.0000 1.0000
1.0000 1.0000 1.0000 1.0000 1.0000
1.0000 1.0000 1.0000 1.0000 1.0000
1.0000 1.0000 1.0000 1.0000 1.0000
0.7112 0.0292 0.2316 0.7962 0.7791
0.2217 0.9289 0.4889 0.0987 0.7150