Matlab 3D surface normal distribution - matlab

I have two normal distributions and i am trying to make a volumetric surface from them. I got the following graph
clear; clc;
nsamp = 100000;
%
% Basic variables
%
m1=2.724;
dp1=0.375;
R = normrnd(m1,dp1, nsamp, 1);
m2=1.345;
dp2=0.135;
S = normrnd(m2,dp2, nsamp, 1);
%
Z = R - S;
I = (Z < 0);
pf = sum(I)/nsamp
beta = -norminv(pf)
%
% Histograms
hist(S,20)
hold on
hist(R,40)
set(findobj('Type','patch'),'Facecolor','none','Edgecolor','black')
set(gca,'Fontsize',18,'Fontname','euclid')
xlabel('R & S')
figure
scatterhist(R,S)
xlabel('R'),ylabel('S')
and i would like to make a 3D surface and the points in red to remain in red and the blue points in blue. Can someone help me? Regards

I can't write a tested solution right now - not access to Matlab license server. But you have two problems here.
The first is to create a surface plot of density. You can use the hist3 function for the this - and if you return the values of the bins you can control the plotting yourself:
[N C] = histc(X)
(I'm sorry it is not clear to me what you really want to plot - is it R, S, Z, or R+S? that's why I used X).
Then to color the graph, you can use patch coloring - see http://www.mathworks.com/help/matlab/visualize/specifying-patch-coloring.html for how to do that.
The combination of these two should get you a long way there... but it's still a little unclear what you really want to do. I hope these hints help you on your way.

Related

Transform a photo according to a function [MATLAB]

I'm a beginner in matlab and I'm trying to transform a photo according to a function given in the code.
My aim is to see where some points of the R^2 plan go. For example, i'd like to transform :
But I can't figure this out.
I found some good conversations on this topic:
https://www.mathworks.com/matlabcentral/answers/81975-is-it-possible-to-pass-an-image-as-an-input-to-a-function-in-matlab
and good functions like :
https://www.mathworks.com/help/images/ref/imtransform.html
https://www.mathworks.com/help/images/ref/imwarp.html
but I don't understand what to do with that because I don't have a matrix but just like the function "1/z"...
The aim is to do something better than this :
How to plot the Wolfram Alpha grid? [MATLAB]
I've tried to add colors to the mesh graph but I ve not succed in doing so... I could only find how to change uniformly the colors, like setting all in green...
If you have another solution not using an image but constructing a
grid of a range of colors and then deforming it (like in the link) or
even better, instead of a grid, creating a whole plan with an uniform
distribution of the colors... it also fixes the problem!
Thank you !
You can use the surf function to plot a grid with colored patches. If you use the same code in my answer to your previous question, you could visualize the original grid with colors as follows:
C = X.^2 + Y.^2; %change this to any function you like to get different color patterns
surf(X,Y,C);
view([0, 90]); %view the mesh from above
Now, if you want to see how the transformed mesh looks like, you can do:
surf(U,V,C);
view([0, 90]);
where U and V are computed according to my previous answer.
Edit: Added sample code for transforming an image using geometricTransform2d and imwarp.
clear
clc
A = imread('peppers.png');
figure(1)
imshow(A)
t1 = geometricTransform2d(#ftransform);
Rin = imref2d(size(A),[-1 1],[-1 1]);
Rout = imref2d(size(A),[-5 5],[-5 5]);
B = imwarp(A, Rin, t1,'OutputView',Rout);
figure(2);
imshow(B)
function Xt = ftransform(X)
Z = complex(X(:,1),X(:,2));
Zt = 1./Z;
Xt(:,1) = real(Zt);
Xt(:,2) = imag(Zt);
end

how to create the surface plot with unequal vector size

clc
clear all
n1=rand(1,10);
n2=rand(1,10);
n3=rand(1,10);
n4=rand(1,10);
m1=rand(1,10);
m2=rand(1,10);
m3=rand(1,10);
m4=rand(1,10);
n=[n1;n2;n3;n4];
m=[m1;m2;m3;m4];
z=[0.1,0.2,0.3,0.4];
I want to create a surface plot using above data.for given z=0.1 plot(n1,m1), for z=0.2 plot(n2,m2)....how to combine all this to get a surface plot?
Mathwork's answer on how to plot scattered data should give you the best start.
I assume that your minimal example is not correct regarding z, because all data has to have the same dimension.
Fixing this, interpolation is one answer to your approach.
Following the mentioned link, it should look like this:
n = rand(4,10);
m = rand(4,10);
z=repmat([0.1 : 0.1 : 0.4]',1,size(n,2));
resolution = 0.01;
[xi,yi] = meshgrid(min(n):resolution:max(n), min(m):resolution:max(m));
zi = griddata(n,m,z,xi,yi);
surf(xi,yi,zi);

how to plot each profile with a distance from the other in a 2D plot?

I want to plot something like this http://www.nature.com/nprot/journal/v9/n6/fig_tab/nprot.2014.090_F7.html or like this one http://file.scirp.org/Html/11-2200285/ff6819f9-5db9-4121-852d-a8d5c302a5a4.jpg I have a 2D matrix.
I tried to plot it like this but did not work
figure;
hold on
for i = 1:size(X,1)
plot(X(i,:)+10)
end
Since you do not specify what exactly does not work, I am having some difficulties to answer your question directly (I do not have enough reputation for a comment).
The following code results in an image like the ones you link. Maybe you forgot to multiply the y-shift with the iteration number i.
N = 200;
x = 1:N;
M = 5;
X = sin(kron(x,ones(M,1)));
figure; hold all;
for i=1:N
plot(X(i,:)+i*5);
end

How to overlay histograms in matlab

I have multiple histograms that I would like to overlay on top of each other but I don't know how to do it. I found the code below but I don't know how to modify it to run on a loop instead of just two histograms.
data1 = randn(100,1); % data of one size
data2 = randn(25, 1); % data of another size!
myBins = linspace(-3,3,10); % pick my own bin locations
% Hists will be the same size because we set the bin locations:
y1 = hist(data1, myBins);
y2 = hist(data2, myBins);
% plot the results:
figure(3);
bar(myBins, [y1;y2]');
title('Mixed size result');
or what is a better way of comparing histograms if they are more than 10 or 20.
Your question is very general. Firstly I do not understand why you insist on a for loop.
Personally I don't like the included bar plot. It quickly gets messy (especially since the bars are not at the "original" location)
If you got a lot of histograms I would consider a stairstep plot as it doesn't fill the plot area so much. Or you could come up with your own - eg using transparent patches.
If it get's lots of curves there are many ways to visualize them google for "multivariate visualization" and be amazed. One of the most amusing ways would be Chernoff faces.
it's much easier now:
histogram(data1, myBins);
hold on;
histogram(data2, myBins);
You could do the following, although it's not the only way:
data = cell(1, N);
y = cell(1, N);
yBar = zeros(N, 10);
for i=1:N
data{1, i} = randn(10*round(rand(1,1)), 1);
y{1, i} = hist(data{1, i}, myBins);
yBar(i, :) = y{1, i};
end
yBar = yBar';
figure(3);
bar(myBins, yBar);
title('Mixed size result');
Using the y cell is not obligatory of course, I left it there to actually show what's happening.
I would suggest this. It's simple and does not require for loops:
bar([y1.' y2.'],'stacked')
Here's a way that was useful to me:
I'm plotting a histogram for each column of the matrix ao.
The code was:
for i = 1:size(ao,2)
[h, y] = hist(ao(:,i), linspace(-5,10,100));
h = i + (0.95./max(h(:))) .* h;
barh(y, h, 'BarWidth', 1, 'BaseValue', i, 'LineStyle', 'none');
hold on;
end
grid;
Note that just changing barh to bar will give the same thing but going up-down instead of left-right (i.e. the figure rotated by 90° anti-clockwise).

Plotting the result of a 2 parameter function in matlab (3D Graph)

Basically, I have a function f(X,Y) that would return one value for each X,Y that I give. Is there any function in matlab where I can pass the function f, the ranges for X,Y so that it plots a 3d graph showing the magnitude of f (along the z axis) for all values within the given range.
ezplot3, does this kind of, but it takes only one parameter 't'. I am very new to matlab and am trying my best to learn it fast, but I couldnt find much regarding this. Any help would be appreciated
Keep in mind, that with matlab, you're never really plotting "functions"; You're plotting arrays/vectors. So instead of trying to plot g = f(X,Y), you'll actually by plotting the vectors X, Y, and g, where X and Y are your original inputs and g is a vector containing your outputs.
I'm having a hard time visualizing what exactly you're trying to plot but basically, you can follow any standard matlab plotting example such as: http://web.cecs.pdx.edu/~gerry/MATLAB/plotting/plotting.html
It does not produce a 3D plot, but I have found the 2D scatter plot useful for this kind of task before:
scatter(x, y, 5, z)
Where z is the value of the function at the point (x, y) will produce something similar to what you want. Its perhaps not quite as pretty as a full 3D plot but can be used to good effect.
See:
http://www.mathworks.com/matlabcentral/fileexchange/35287-matlab-plot-gallery-scatter-plot-2d/content/html/Scatter_Plot_2D.html
Here is some (very ugly) code I put together to demonstrate the difference:
j=1;
y = -100:1:100;
for i = -100:1:100
y = [y -100:1:100];
count = 0;
while count < 202;
x(j) = i;
j = j+1;
count = count + 1;
end
end
z = (abs(x) + abs(y));
figure(1)
scatter(x, y, 10, z)
h=colorbar;
figure(2)
ezsurf('(abs(x) + abs(y))')
Well, this is what I was going for : http://www.mathworks.com/help/matlab/ref/ezsurf.html
if i do this
ezsurf('f(x,y)');
I get the 3d graph I wanted.
Thanks anyways!