0

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.

1
  • possible duplicate of Line plots in Matlab. Duplicate user, near duplicate question. Commented Jul 5, 2010 at 4:23

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

@gnovice would be my answer.

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

imagesc(A)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.