MATLAB: Plot a 4th dimension as color

3d plots4th dimensionvariables

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 31×31 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

Best Answer

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));
Related Question