Reputation: 1
I've got a matrix A=magic(4) and wish to plot the values using plot3(1:4,1:4,A,'ks'). But that plots everything on the diagonal and not where they actually are relative to the other values in the matrix. How do I do that? I'm sure it's pretty easy but I'm new to matlab.
Upvotes: 0
Views: 1037
Reputation: 1050
@gnovice would be my answer.
I'll add that sometimes a simple imagesc is nice for visualizing a matrix:
imagesc(A)
Upvotes: 1
Reputation: 125854
You can use MESHGRID to generate matrices for the X
and Y
coordinates of the plotted points:
[X,Y] = meshgrid(1:4); %# X and Y are each 4-by-4 matrices, just like A
plot3(X,Y,A,'ks'); %# Make a 3-D plot of the points
You could also plot a surface instead of a set of points using the function SURF, in which case the need to use MESHGRID to generate the X
and Y
coordinates is optional:
surf(X,Y,A); %# Use the 4-by-4 matrices from MESHGRID
surf(1:4,1:4,A); %# Pass 1-by-4 vectors instead
surf(A); %# Automatically uses 1:4 for each set of coordinates
Upvotes: 2