how to plot a matrix on a 3D graph

4 次查看(过去 30 天)
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.

采纳的回答

Sean de Wolski
Sean de Wolski 2011-6-10
[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 个评论
VELMURUGAN K
VELMURUGAN K 2020-6-4
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.

请先登录,再进行评论。

更多回答(2 个)

krivan
krivan 2011-6-10
Can you please clarify which is doing what especially in the argument of the reshape and what exactly xx(:) does for example. I am just new to Matlab and couldn't find it in the manual.
Thanks.

krivan
krivan 2011-6-20
Brilliant! Thanks for your help!

类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by