MATLAB: How to plot a matrix on a 3D graph

3d plotmatrix

Hi,
I have a 6×6 matrix that I need to plot on an 3D graph. The elements 1-2-3-4-5 of the first column should be on the x axis, the elements 1-2-3-4-5 of the first row should be on the y axis, element 0,0 is NaN.
The remaining 5×5 array contains the values for the z axis.
Can somebody please help me how to plot it.
Thanks.

Best Answer

[xx yy] = meshgrid(M(1,2:end),M(2:end,1));
plot3(xx(:),yy(:),reshape(M(2:end,2:end),[],1));
or surf or mesh whatever type of plot you want.
Addendum The best way to learn is to do it yourself and break down every line to see what's happening.
%Sample script to identify every op.
M = magic(6)
[xx yy] = meshgrid(M(1,2:end),M(2:end,1))
disp('M(2:end,2:end) = ');
M(2:end,2:end)
disp('M reshaped to column vector: ')
reshape(M(2:end,2:end),[],1)
M =
35 1 6 26 19 24
3 32 7 21 23 25
31 9 2 22 27 20
8 28 33 17 10 15
30 5 34 12 14 16
4 36 29 13 18 11
xx =
1 6 26 19 24
1 6 26 19 24
1 6 26 19 24
1 6 26 19 24
1 6 26 19 24
yy =
3 3 3 3 3
31 31 31 31 31
8 8 8 8 8
30 30 30 30 30
4 4 4 4 4
M(2:end,2:end) =
ans =
32 7 21 23 25
9 2 22 27 20
28 33 17 10 15
5 34 12 14 16
36 29 13 18 11
M reshaped to column vector:
ans =
32
9
28
5
36
7
2
33
34
29
21
22
17
12
13
23
27
10
14
18
25
20
15
16
11
The xx(:) operation does the same thing as reshape in this use of reshape.