tags:

views:

22

answers:

2

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.

+2  A: 

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
gnovice
A: 

@gnovice would be my answer.

I'll add that sometimes a simple imagesc is nice for visualizing a matrix:

imagesc(A)
Andrew B.

related questions