i have 100*100 matrix, how can i make plot3 graph? - matlab

I have a 100 x 100 matrix and i have to use plot3 in MATLAB environment to graph this data. I tried plot3(matrix name) but I faced this error "not enough input arguments". I think plot3 needs 3 input arguments, but I only have this matrix of data. could anyone help me to solve this problem? Is there any alternative for plot3 when we don't have enough arguments?
I need a graph like this:

I think you want to plot the values in a figure as a sort of surface element. What you can do then is:
[X,Y] = size(matrix);
figure;
surface(1:X,1:Y,matrix);
What this does is that it creates a vector for both X and Y indices, as possible in surface. The X and Y indices are obtained by setting them as integers from 1:size, so basically you assign the location of each matrix element to an index.
Note that you can strictly speaking use surface(matrix) as well, but the former approach allows you to use custom indexing, as long as the lengths of the vectors X and Y are the same as the size of your matrix.
For the waterfall use:
figure;
waterfall(matrix);
Sample code:
A=rand(100);
figure;
waterfall(1:100,1:100,A);
Gives:
where you can play around with the name-value pairs, see the documentation on that.

I think what you need is mesh or surf instead of plot3.
plot3 draws a line in 3d-space, so it will need three vectors of the same length (one for each dimension).
When you have a matrix, one reasonable way of displaying it is as a surface in 3d space, which is done by the functions mesh and surf.
Try it out! I hope i helps!

Related

Is there a way in matlab to plot several 1D courves on specific coordinates over a 3D mesh?

For example, I got 3 pairs of 1-D loglog curves and additionally their associated cartesian coordinate points (x,y,z) of one of their ends A, B and C over a mesh surface S (z is positive downwards and linear but coincides in direction with the log(y)-axis from the curves). Is it possible to respresent in a single figure such system of plots in matlab?
Moreover, obtain an interpolated slice from A,B and C?
The images from the question of user3281667 gives an insight of what we are trying to do here:
https://gis.stackexchange.com/questions/252939/interpolating-xyz-data-in-arcgis-3d-analyst
Thanks.
Kind of solved. First we need to know in which format is our data, this case scattered.
I concatenated a nx4 matrix with the preprocessed data A=[X Y Z C].
Then use the right tools, to plot use scatter3: scatter3(A(:,1), A(:,2), A(:,3),30, A(:,4), 'filled' )
Now to interpolate, fisrt generate a grid refinement with meshgrid: [Xm, Ym, Zm] = meshgrid(min(X):2:max(X), min(Y):2:max(Y), min(Z):2:max(Z)) next interpolate using griddata Cm = griddata(X,Y,Z,C,Xm,Ym,Zm);and last plot again.
figure
scatter3(Xm(:), Ym(:), Zm(:), 30, Cm(:), 'filled' )
Thanks to user7431005

Creating 2D points near y=x

I need to generate some random 2D points (for example 30 points) near the y=x line, insert them in a matrix, plot it and then calculate the SVD of the matrix. But since I'm new to MATLAB I don't know how can I generate my desired matrix.
Since this looks like homework I'll just post some general ideas here.
randi can be used to get semi-random integers. Using that you can create a 2D matrix by duplicating the array and putting them together. Thus: generate a 30x1 column and duplicate it to a 30x2 column. All rows will have the same two entries, i.e. x=y.
Noise can be added to this by creating a 30x2 matrix of random numbers, use rand for that and simply add that to the previously created matrix.
Check the documentation on svd to see how the singular-value decomposition works, it's fairly straight-forward if you know your linear algebra.
Finally for plotting you can use various tools such as image, imagesc, plot, surf and scatter, try them and see which works best for you.
Here is a quick example I made: https://saturnapi.com/fullstack/2d-points-randomly-near-line
%// Welcome to Saturn's MATLAB-Octave API.
%// Delete the sample code below these comments and write your own!'
x = 13 + 6.*rand(20,1);
y = x*0.7 + 0.5*rand(20,1);
[X,Y] = meshgrid(x,y)
figure(1);
plot(x,y,'.');
%// Print plot as PNG with resultion of 60 pixels per inch
print("MyPNG.png", "-dpng", "-r60");

Matlab interp1 curve doesn't follow data

I've been using interp1 to plot curves to follow sets of datapoints, and for most of the datapoints it's been working:
But when I try it with another set of datapoints it doesn't follow them at all:
For both interpolations the code I'm using is just:
curve = interp1(x, y, 'pchip');
Where x is just a set of numbers that correspond to the x axis of each datapoint, and y is the values themselves.
I can't tell what is different about the second dataset that is causing the interp1 function to not follow the data.
So with thanks to #m.s. for providing his code, it turns out the issue is that with the second graph I was interpolating with x= -90:10:90, whereas if I interpolate with 1:19, in a similar manner to the first graph, then the problem is fixed.

Creating a 3D function in Matlab using meshgrid

This should be a really simple question, but for some reason I'm getting unreasonably confused and the Matlab documentation isn't helping.
Given a uniform grid of coordinates (x_i,y_j,z_k), I want to make a 3-dimensional array F in Matlab such that F(i,j,k)=f(x_i,y_j,z_k). The following is obviously incorrect:
x=linspace(-1,1,100) % uniform mesh on [-1,1]^3
[X,Y,Z]=meshgrid(x);
f=X.*Y.*sin(pi*Y.*Z) % for example
Do I need to use permute somewhere? I know that I could simply make a triple loop, but as we know that is slow.
Thanks!
Use ndgrid instead of meshgrid to avoid the unwanted permutation between first and second dimensions.
From the documentation (see also here):
MESHGRID is like NDGRID except that the order of the first two input
and output arguments are switched (i.e., [X,Y,Z] = MESHGRID(x,y,z)
produces the same result as [Y,X,Z] = NDGRID(y,x,z))

MATLAB: Need to make a 4D plot (3D + Colour/Color)

I need to make a 3D surface where colour will represent the fourth variable. I know "surf" is SIMILAR to what I need, but that's not quite it. Basically, I have the following variables:
t = [1:m]
y = [1:n]
a = [1:o]
These should be the three Cartesian corodinate axes.
I also have a variable S that is of dimensions m x n x o, and is basically the amplitude, a function of the previous three variables (i.e. S = f(t,y,a)). I want this to be represented by colour.
So to summarize, I need a graph of the form (t,y,a,S), where the first three variables are vectors of unequal sizes and the final variable is a multidimensional array whose dimensions are determined by the first three.
Thanks in advance.
SCATTER3 requires x, y and z and other grouping arguments to be equally-sized Nx1 vectors for a single series or NxM matrices for M series.
You have full space 3D data. To make equally-sized coordinate vectors use MESHGRID (or NDGRID) function:
[X, Y, Z] = meshgrid(t, y, a);
Then you can use SCATTER3:
scatter3( X(:), Y(:), Z(:), [], S(:) )
The problem is since it's full space data scatter3 will not be helpful specially if you have a lot of points.
You can probably filter your S variable (something like idx = S > 0), then you can plot filtered data.
If you really need to visualize all the data, look at Volume visualization in MATLAB documentation. I can recommend SLICE function, for example.
EDIT
Here is an example of full 3D space scatter plot for small vectors (m, n, o equal to 5) with S = rand([m,n,o]); scatter3( X(:), Y(:), Z(:), [], S(:), 'filled' )
EDIT 2
From your comments to the other answer I found that you have 32x76050x4 matrix. You can actually plot 2D slice one at a time. you can do it in 2D with IMAGESC function, or in 3D with SLICE function.
Try:
imagesc(S(:,:,k))
where k is a number from 1 to 4 for the 3rd dimension.
Or try
slice(S, [], [], 1:size(S,3))
shading flat
Maybe this user-created plotting routine can help.
Screnshot from the linked page:
I've always used scatter3 for coloring/sizing pixels in 3d space. I believe the signature is:
scatter3(x,y,z, size, color)
The size and color can be scalar or vector of length equal to the coordinates. I usually use either the color or the size to reflect the fourth attribute, depending on what I'm showing. I don't have Matlab on this machine, so forgive me if my memory isn't completely accurate on the usage. "help scatter3" should describe it much better.