How to plot a zero-one matrix that will look like scatter? - matlab

I have a matrix of zeros and ones and I want to plot the ones in their location in the matrix. So that it will look like the matrix but instead of ones a marker and instead of zeros nothing.
Is there a function for doing this or I need to get the x and y for every one and then just do a simple scatter plot?
Thank you for the help!

Try the function spy, it plots a blue dot for every non-zero entry of a matrix.

imagesc
is an approach for this that I find useful.

Another option is to get coordinates of non-zero elements with FIND:
[x,y] = find(A);
scatter(x,y)
It's the same way as used by SPY, just with a little more control. In opposite to PLOT or SCATTER, SPY does not return points handle, which anyway can be retrieved by FINDOBJ.

Related

Creating a matrix of plots in MATLAB, similar to plot matrix

I want to create a 3x3 matrix of plots in MATLAB where each element is a subplot of some corresponding 1000 samples (suppose values are stored in a 3x3x1000 matrix A). I want the subplots to be spaced very close together, and the y axis numbers/labels to only show up for the left subplots and the x axis numbers/labels to only show up for the bottom subplots.
For example, something similar to plotmatrix, except subplot ij would plot A(i,j,:).
plotmatrix example
Is there an easy way to do this in MATLAB? Thanks!
The axes properties of the subplots can be modified to achieve this, but an easier way would be to use a FEX submission called panel. Have a look at its example output:
subplot can do that for you in MATLAB.
h = subplot(3,3,1)
Will split the current figure in a 3 x 3 matrix and creates an axes(area where you plot something) in the 1st cell of the matrix. h is the "handle" to the axes which you can then use to modify the xlabels and ylabels in any way.
Documentation for subplot
Documentation for Axes

Matlab -plot a vector field

I have written a function in Matlab that gives me a vector at a position (x,y,z).
Now I am looking for the easiest way to make a colored map of this field on a grid and the color should be related to the norm of the vector.
What is the easiest way to define such a grid for $x \in [x_0,x_1],y \in [y_0,y_1], z \in [z_0,z_1]$? Probably linspace for each component would be possible, but maybe there is already a command that gives me the grid.
Now I need to evaluate my function. The problem is, that it actually gives me two vectors, but I am only interested in the first one. So when I first tried to do this I thought that $[A(i,j,k),~]=function(x(i),y(j),z(k))$ could work, but it did not(My goal was: Choose the first vector(A) and mark him with the reference(i,j,k), so that you later on know to which coordinates this vector belongs to).
So I am highly interested in any kind of ideas.
The function meshgrid might be what you are looking for to generate the x, y and z coordinates.
Instead of
[A(i,j,k),~]=function(x(i),y(j),z(k));
try
[A(i,j,k,:),~]=function(x(i),y(j),z(k));
so that you can fit the entire size of the 3-coordinate vector. Also, if you want to preallocate space use
A = zeros(Nx,Ny,Nz,3);
where Nx,... are the dims of your coordinate space. Then like #Moly explains, use meshgrid to generate a 3D grid,
[X Y Z] = meshgrid(x,y,z);
and loop or vectorize to resolve values of your function at points X(i,j,k),Y(i,j,k),Z(i,j,k), compute the norm and store it in 3D array C.
Edit
Representing a cube with mesh(X,Y,Z,C) is not possible but individual slices of the 3D cube can be visualized with mesh, setting the height Z equal to the result of the function C. Some additional work is then required to get coloring right.
Yet another alternative is to use pcolor or contourf. This is perhaps the easiest way to show the 4D data beyond creating a 3D isosurface.
code:
figure
colormap(jet)
for ii=1:9
subplot(3,3,ii)
pcolor(X(:,:,ii),Y(:,:,ii),F(:,:,ii))
axis('equal','square'), title(['Z=' num2str(ii)])
caxis([0 1])
end

How to plot a list of vectors as a sphere?

I have an array of normalized vectors (1538 of them) forming a sphere. Also I has an array of numbers of the same size 1538. I want to plot something like this:
I tried the sphere and surf functions but I can't find a way to use my vectors. I figured there should be some way to do this.
Thanks a lot.
I think you can use delaunay to create a triangulation and plot that using trimesh or trisurf.
Both trimesh as trisurf accepts a fourth argument to specify the color of each vertex, add the option 'facecolor','interp' to interpolate the color of each face between vertices.
edit: I experimented a bit further on it, and since it's a sphere, I think convhull is better suited.
Example:
[x,y,z]=sphere(25);
x=x(:);y=y(:);z=z(:);
tri = convhull([x y z]);
C = cos(y);
trisurf(tri,x,y,z,C,'facecolor','interp');
instead of C in the example you can use your own vector of values to specify the color

How does MATLAB's normpdf function work?

When trying to plot a normal PDF with mean=0 and standard deviation=20 using the MATLAB command normpdf() I get weird results, see picture.
The code used to plot the figure is as follows:
plot(normpdf((-100:0.1:100),0,20))
What is the correct way of using this function?
When you call plot with ONE argument, it plots those numbers on the y axis, using the index numbers of those values for the x axis. If you wanted the x axis scaled properly, you had to provide them in the first place. Thus...
x = -100:0.1:100;
plot(x,normpdf(x,0,20),'-')
I assume you expected the x-axis to be centered at 0? You need to specify an x-vector for plot. Try plot([-100:0.1:100], normpdf((-100:0.1:100),0,20));.

How to make a log plot in matlab

Is it possible to make a plot in matlab that does not actually take the logs of the values? I'm plotting wide ranges of values and when I try to make a log plot of them, those below 1 become negative. I would just like it to plot the values on a log scale without taking their logs.
Alternatively, set(gca,'XScale','log') if you have your plot already.
Yes, it is possible. Use the loglog command.
The example from the Mathworks website:
x = logspace(-1,2); % generate a sequence of points equally spaced logarithmically
loglog(x,exp(x),'-s')
grid on
If you do not want both axes to be log scale, use semilogx or semilogy.
So, you want to plot liner data on logarithmic axes? You can exponentiate you values before using the log plot. This way the point p=(10,3) will plot at the x=10 position.