how to plot a matrix on a 3D graph
2 views (last 30 days)
Show older comments
Hi,
I have a 6x6 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 5x5 array contains the values for the z axis.
Can somebody please help me how to plot it.
Thanks.
0 Comments
Accepted Answer
Sean de Wolski
on 10 Jun 2011
[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.
1 Comment
VELMURUGAN K
on 4 Jun 2020
Hi,
I have a 15x15 matrix that I need to plot on an 3D grid or surface graph. The elements 1-15 of the first column should be on the x axis, the elements 1-15 of the first row should be on the y axis, element 0,0 is NaN.
The remaining 14x14 array contains the values for the z axis.
Can somebody please help me how to plot it.
give the exmaple plot also
Thanks.
More Answers (2)
See Also
Categories
Find more on Discrete Data Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!