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 CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by