Plot a 4th dimension as color

10 ビュー (過去 30 日間)
Rodrigo
Rodrigo 2013 年 7 月 11 日
Hello,
I'm new using matlab and I'm having some troubles, I hope someone can help me.
Until now I was working with two var functions, like F=x+y; x and y goes from 1 to 4 so I used [x,y]=meshgrid(1:0.1:4,1:0.1:4) and then run the function F to obtain the values of F 31x31 and make the plot using mesh(x,y,F).
Now I need to incorporate a new var to F, like F=x+y+z. I would like to see F as surface like in the two var case and set the z var as a color. It's possible? I have been searching the forum without luck. I have also tried doc surf and doc scatter3 but I keep getting errors =( when I try to do something with [x,y,z]=meshgrid(1:0.1:4,1:0.1:4,1:0.1:4) and F 31x31x31.
please if someone could indicate me how to make this happen, I would appreciate

採用された回答

Image Analyst
Image Analyst 2013 年 7 月 12 日
編集済み: Image Analyst 2013 年 7 月 12 日
I don't think you want or need a 4th dimension. You only need 2 dimensions. Dimension 1 = row, dimension 2 = column, value = index into a lookup table. In other words, you make up an indexed image where the value of F at (y, x) is an index into a pseudocolor lookup table. Try this code:
% Define image size.
rows = 300;
% Make x and y
[x, y] = meshgrid(1:rows, 1:rows);
% Make the z values.
% Let's use the famous "peaks" function.
zValues = 20*peaks(rows);
% Make "F" an indexed image.
F = zeros(rows, rows, 'double');
for x = 1 : rows
for y = 1 : rows
F(y, x) = x + y + zValues(y, x);
end
end
imshow(F, []);
% Apply colormap called "jet".
colorbar
colormap(jet(256));
  1 件のコメント
Rodrigo
Rodrigo 2013 年 7 月 12 日
Mmm is not what I needed and I can't even make it work with my own functions and variables. Anyway I really appreciate your answer and effort, I'm just too new to mathlab to make it work thanks.

サインインしてコメントする。

その他の回答 (1 件)

Shashank Prasanna
Shashank Prasanna 2013 年 7 月 11 日
When using surf to plot the meshgrid, specify the C to be the color or the 4th dimension.
  1 件のコメント
Rodrigo
Rodrigo 2013 年 7 月 11 日
Thanks for your reply Shashank.
I'm sorry, but could you tell me exactly what to do?. I'm really new in mathlab and I can't pull it off with your explanation + the surf page.
I just get tons of errors at this point =(
??? CData must be an M-by-N matrix or M-by-N-by-3 array Error in ==> graph3d.surfaceplot.surfaceplot>localConstructor at 136 h = graph3d.surfaceplot(argin{:}); Error in ==> graph3d.surfaceplot.surfaceplot at 7 h = localConstructor(varargin{:}); Error in ==> surf at 101 hh = double(graph3d.surfaceplot(args{:},'parent',parax));

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by