Matlab- Given matrix X with xi samples, y binary column vector, and a vector w plot all these into 3d graph - matlab

I have started to learn Machine Learning, and programming in matlab.
I want to plot a matrix sized m*d where d=3 and m are the number of points.
with y binary vector I'd like to color each point with blue/red.
and plot a plane which is described with the vertical vector to it w.
The problem I trying to solve is to give some kind of visual representation of the data and the linear predictor.
All I know is how to single points with plot3, but no any number of points.
Thanks.

Plot the points using scatter3()
scatter3(X(y,1),X(y,2),X(y,3),'filled','fillcolor','red');
hold on;
scatter3(X(~y,1),X(~y,2),X(~y,3),'filled','fillcolor','blue');
or using plot3()
plot(X(y,1),X(y,2),X(y,3),' o','MarkerEdgeColor','red','MarkerFaceColor','red');
hold on;
plot(X(~y,1),X(~y,2),X(~y,3),' o','MarkerEdgeColor','blue','MarkerFaceColor','blue');
There are a few ways to plot a plane. As long as w(3) isn't very close to 0 then the following will work okay. I'm assuming your plane is defined by x'*w+b=0 where b is a scalar and w and x are column vectors.
x1min = min(X(:,1)); x2min = min(X(:,2));
x1max = max(X(:,1)); x2max = max(X(:,2));
[x1,x2] = meshgrid(linspace(x1min,x1max,20), linspace(x2min, x2max, 20));
x3 = -(w(1)*x1 + w(2)*x2 + b)/w(3);
surf(x1,x2,x3,'FaceColor',[0.6,0.6,0.6],'FaceAlpha',0.7,'EdgeColor',[0.4,0.4,0.4],'EdgeAlpha',0.4);
xlabel('x_1'); ylabel('x_2'); zlabel('x_3'); axis('vis3d');
Resulting plot

Related

Generate a 3D scatter plot from 3D Matrix - Matlab [duplicate]

This question already has an answer here:
Plotting volumetric data in MATLAB
(1 answer)
Closed 4 years ago.
I have given a 300x300x300 Matrix.
The first subscript represents the x-value, the second the y-value and the third the z-value.
To access a value of a specific point in the matrix i use:
matrix(x-val, y-val, z-val)
I want to create a 3D scatter plot where the color of the dots in the plot changes based on the values of the points in the matrix. All values are >=0
As i am pretty new to Matlab i have no idea where to start.
MathWorks has a page that summarizes types of MATLAB plots. I've referenced it on multiple occasions. The function you are looking for is scatter3(X,Y,Z,S,C). Walk through the function's example, it should help you out.
I'm not sure how you can have a 300x300x300 matrix for a point cloud in 3-D. I will assume you have a 300x300x3 matrix, i.e.:
x = matrix(:,:,1);
y = matrix(:,:,2);
z = matrix(:,:,3);
First of all, you probably want to rearrange points into a 2D matrix:
m = reshape(matrix, numel(matrix(:,:,1), 3);
n = size(m,1);
Your matrix is now arranged as a n-by-3 matrix, coloumn 1, 2 and 3 representing x-, y- and z-axis respectively, i.e.:
m = [ x1 y1 z1]
[ x2 y2 z2]
[ ... ]
[ xn yn zn]
Then you can create a basic 3D scatter plot:
scatter3(m(:,1), m(:,2), m(:,3))
However, this is not what you want, because points are in the same colour. To add colour based on your colouring logic, you should firstly create a color matrix using one of the MATLAB's built-in color maps. Here I use jet:
myc = jet(n);
You can also create your own colour map ofc. Elements in the colour matrix are simply normalised rgb values.
Now you will have to weight each points using your own logic:
weighting = myWeightingLogic(m);
weighting will be a n-by-1 vector and it should be normalised if it is not yet.
weighting = weighting/max(weighting);
Now you can colour your scatter plot:
scatter3(m(:,1), m(:,2), m(:,3)), [], myc(round(weighting*n),:));
The full code:
m = reshape(matrix, numel(matrix(:,:,1), 3);
n = size(m,1);
myc = jet(n);
weighting = myWeightingLogic(m);
weighting = weighting/max(weighting);
scatter3(m(:,1), m(:,2), m(:,3)), [], myc(round(weighting*n),:));

plot a 3D matrix of concentrations in matlab with slice

I have a 3D matrix C=51x51x11 dimensions, obtained from a function in a separate script, the x,y,z represent length, depth and height, and the value represent a concentration per x,y,z point. I want to create a slice crossing x and another crossing y showing the difference in concentration by color. I have tried using ngrid and meshgrid but didn't work. may i have some help with this please?
Use slice()
C = randi(1,[51,51,11]);
x= 25; y = 25; z = 5;
sl = slice(C,x,y,z);
Using slice inside a function to make it easy to view in 3d:
function eslice(V,sx,sy,sz)
slice(V,sx,sy,sz)
shading interp
axis equal
axis vis3d
end
This is from my personal library, enjoy.

matlab scatter plot using colorbar for 2 vectors

I have a two columns of data. X = Model values of NOx concentrations and Y = Observations of NOx concentrations. Now, I want to scatter plot X, Y (markers varying with colors) as well as the colourbar which would show me the counts (i.e. number of data points in that range). X and Y are daily data for a year, i.e. 365 rows.
Please help me. Any help is greatly appreciated.
I have attached a sample image.
If I understand you correctly, the real problem is creating the color information, which is, creating a bivariate histogram. Luckily, MATLAB has a function, hist3, for that in the Statistics & Machine Learning Toolbox. The syntax is
[N,C] = hist3(X,nbins)
where X is a m-by-2 matrix containing the data, and nbins is a 1-by-2 vector containing the number of bins in each dimension. The return value N is a matrix of size nbins(1)-by-nbins(2), and contains the histogram data. C is a 1-by-2 cell array, containing the bin centers in both dimensions.
% Generate sample data
X = randn(10000, 1);
Y = X + rand(10000, 1);
% Generate histogram
[N,C] = hist3([X,Y], [100,100]);
% Plot
imagesc(C{1},C{2},N);
set(gca,'YDir','normal');
colormap(flipud(pink));
colorbar;
Result:

Draw a line with non-Cartesian coordinates in MATLAB

MATLAB's surf command allows you to pass it optional X and Y data that specify non-cartesian x-y components. (they essentially change the basis vectors). I desire to pass similar arguments to a function that will draw a line.
How do I plot a line using a non-cartesian coordinate system?
My apologies if my terminology is a little off. This still might technically be a cartesian space but it wouldn't be square in the sense that one unit in the x-direction is orthogonal to one unit in the y-direction. If you can correct my terminology, I would really appreciate it!
EDIT:
Below better demonstrates what I mean:
The commands:
datA=1:10;
datB=1:10;
X=cosd(8*datA)'*datB;
Y=datA'*log10(datB*3);
Z=ones(size(datA'))*cosd(datB);
XX=X./(1+Z);
YY=Y./(1+Z);
surf(XX,YY,eye(10)); view([0 0 1])
produces the following graph:
Here, the X and Y dimensions are not orthogonal nor equi-spaced. One unit in x could correspond to 5 cm in the x direction but the next one unit in x could correspond to 2 cm in the x direction + 1 cm in the y direction. I desire to replicate this functionality but drawing a line instead of a surf For instance, I'm looking for a function where:
straightLine=[(1:10)' (1:10)'];
my_line(XX,YY,straightLine(:,1),straightLine(:,2))
would produce a line that traced the red squares on the surf graph.
I'm still not certain of what your input data are about, and what you want to plot. However, from how you want to plot it, I can help.
When you call
surf(XX,YY,eye(10)); view([0 0 1]);
and want to get only the "red parts", i.e. the maxima of the function, you are essentially selecting a subset of the XX, YY matrices using the diagonal matrix as indicator. So you could select those points manually, and use plot to plot them as a line:
Xplot = diag(XX);
Yplot = diag(YY);
plot(Xplot,Yplot,'r.-');
The call to diag(XX) will take the diagonal elements of the matrix XX, which is exactly where you'll get the red patches when you use surf with the z data according to eye().
Result:
Also, if you're just trying to do what your example states, then there's no need to use matrices just to take out the diagonal eventually. Here's the same result, using elementwise operations on your input vectors:
datA = 1:10;
datB = 1:10;
X2 = cosd(8*datA).*datB;
Y2 = datA.*log10(datB*3);
Z2 = cosd(datB);
XX2 = X2./(1+Z2);
YY2 = Y2./(1+Z2);
plot(Xplot,Yplot,'rs-',XX2,YY2,'bo--','linewidth',2,'markersize',10);
legend('original','vector')
Result:
Matlab has many built-in function to assist you.
In 2D the easiest way to do this is polar that allows you to make a graph using theta and rho vectors:
theta = linspace(0,2*pi,100);
r = sin(2*theta);
figure(1)
polar(theta, r), grid on
So, you would get this.
There also is pol2cart function that would convert your data into x and y format:
[x,y] = pol2cart(theta,r);
figure(2)
plot(x, y), grid on
This would look slightly different
Then, if we extend this to 3D, you are only left with plot3. So, If you have data like:
theta = linspace(0,10*pi,500);
r = ones(size(theta));
z = linspace(-10,10,500);
you need to use pol2cart with 3 arguments to produce this:
[x,y,z] = pol2cart(theta,r,z);
figure(3)
plot3(x,y,z),grid on
Finally, if you have spherical data, you have sph2cart:
theta = linspace(0,2*pi,100);
phi = linspace(-pi/2,pi/2,100);
rho = sin(2*theta - phi);
[x,y,z] = sph2cart(theta, phi, rho);
figure(4)
plot3(x,y,z),grid on
view([-150 70])
That would look this way

Computing and plotting pdf of U as a function of two uniformly distributed random variables

I am working on a joint pdf problem in which the random variable
U = sqrt(X^2+Y^2)
X and Y are uniformly distributed over (-2,2). I want to plot joint pdf of X and Y. Then compute pdf of U and plot it as well. I am using matlab R2011a, and so far, I have come up with the following code. On running the code I got an error message
Undefined function or method 'makedist' for input arguement type 'char'.
I found out that makedist is not on 2011 version. So I tried using
a=-2;
b=2;
X=a+(b-a)*rand(-10,10);
Y= a+(b-a)*rand(-10,10).
However, I am not sure how to compute pdfs of X and Y, and then joint pdf of XY from this. Any help, partial or holistic, is appreciated.
Here is the matlab code for the problem
%% Create distribution objects for X~U(-2,2) and Y~U(-2,2)
pdx=makedist('Uniform','lower',-2,'upper',2);
pdy=makedist('Uniform','lower',-2,'upper',2);
%Compute the pfs
x_ref=-10:1:10;
y_ref=-10:1:10;
pdf_x=pdf(pdx,x_ref);
pdf_y=pdf(pdy,y_ref);
% Plot the pdfs
figure 1;
stairs(x_ref,pdf_x,'g','Linewidth',2);
hold on;
stairs(y_ref,pdf_y,'r','Linewidth',2);
ylim([0 1.5]);
hold off;
% Joint pdf of x and Y
pdfXY=pdf_x*pdf_y;
figure 2;
plot(pdfXY);
%CDF and PDF of U
U=sqrt(X^2+Y^2);
Umin=0;
Umax=sqrt(b^2+b^2);
a=lower;
b=upper;
x=sqrt(U^2-Y^2);
xmin=0;
xmax=x;
ymin=0;
ymax=U;
Ucdf=integral2(pdfXY,xmin,xmax,ymin,ymax);
% plot CDF of U
figure 3;
plot(Ucdf)
I am just looking to plot the regions than for any specific sample set. X and Y are continuous independent uniform random variables.
As your x and y are independent at random, the theoretical joint distribution is just a product of the two
P(x,y) = P(x)*P(y)
In terms of MATLAB code, you may think of x and y running along two different dimensions:
N = 10; %// think of a probability mass function over N points
x = linspace(-2,2, N);
y = linspace(-2,2, N)';
Px = ones(N,1)./N;
Py = ones(1,10)./N;
%// Then the joint will be:
Jxy = bsxfun(#times, Px , Py);
figure
pcolor(x,y,Jxy)
You can now plug whatever distribution you like, if they are independent for Px and Py, and it will work