Color a grid by using an intensity matrix

Hi all! I need to color a grid according to the values of a specific matrix. In particular, I have a 5x5 matrix representing an intensity value (from 0 to 1) and each value A(i,j) of this matrix is related to a specific cell in the grid. The grid goes from 0 (meters) to 100 (meters)---> 0:20:100 for both axes. Thus, each 20m x 20m cell should have a color according to the matrix described above. I tried with pcolor(), imshow(), imagesc() and so on, but with bad results.
Can you help me? Thanks in advance!!! Giulio (Sapienza University of Rome)

 Accepted Answer

What do you mean "bad results"? Can you explain or show what you're doing?
Either way, I'm going to take a guess that the problem is related to the fact that you know the intensities are between 0 and 1, but imagesc always scales to the range of the data. But using image won't work either, because it uses actual values to index into the colormap. If that's the issue, then here are a couple of ways around it:
z = rand(5);
x = 10:20:90;
image(x,x,cat(3,z,z,z))
This makes a 3-D array out of z, then makes a true-color image. Or:
figure
colormap(gray(64))
image(x,x,0.5+z*64), colorbar
which sets the colormap to use 64 colors, then simply expands the possible data range to match that (1 to 64).

More Answers (4)

Thanks for your reply! the first solution you proposed is what I was looking for... the problem now is that the y axis goes from 100 (bottom) to 0 (top) so I need to reverse it... how can I change the colormap? I tried colormap(autumn(3)) but anything changed... Thanks a lot!

1 Comment

The first problem is easily fixed with "axis xy"
The colormap is irrelevant if you are using a true-color image (which is what my first solution does). By making all three planes of the image equal to z, you make a grayscale image. If you want some other color scheme, you'll have to work out how to make the 3-D array. For example
image(x,x,cat(3,0*z,0.8*z,z))
But it might just be easier to use the second approach I showed.

Sign in to comment.

I used the second approach and all the "problems" are now fixed! Thanks! I really appreciated your help!
I have another simple question... what if is 10 x 10? (0:10:100 for both ax.) How do I modify this expression?
x = 10:20:90;

2 Comments

ok this is not a simple question but a stupid question :) the answer is 5:10:95...
Not a stupid question, but glad you figured it out. For anyone else out there: the point is that image centers the pixels on the axis values, hence the x values have to be the *centers* of the squares, not the edges.

Sign in to comment.

2D visualization is definitely ok, but what if I want to plot this matrix in 3D? I now have a 10x10 matrix with values in [0,1] but when I try 'surf(my_matrix)' MATLAB only displays a 9x9 grid. Do I have to use meshgrid command before? Thank you in advance!

8 Comments

Can you explain the problem more? Do you get a surface, but it's only 9x9 instead of 10x10? If so, what are the values in my_matrix?
I'll give you an example with a 5x5 matrix:
0.1 0.2 0.9 0.2 0.3
0.5 0.5 0.6 0.7 0.4
1 0.8 0.5 0.2 0.1
0.2 0.3 0.4 0.5 0.6
1 1 0.5 0.9 0.8
When I run surf(matrix_above) the result is a 3D plot where height (z axis) and color are given by the values of the matrix, but the x,y axes go from 1 to 5 (so only 4 elements of the matrix are represented). They do not start from 0 so the last row and last column of the matrix are not in the plot. Is the problem clear? thanks!
I think this is just a confusion about vertices versus faces of a surface plot. The values in the matrix are plotted as the vertices of the surface. Look along the leading edge of the plot and you'll see there are five corners. The patchwork in between, however, will have four patches that interpolate between the corners.
If you do imagesc(matrix) instead, you'll see a 5-by-5 grid of color patches, centered at the index values.
Thanks for your answer but I really need to represent this matrix in 3D... imagesc(..) is two-dimensional only... As you said, the values in the matrix are plotted as the vertices of the surface... Is there a way to modify the matrix in order to view all the matrix ? thanks!
surf() data data and set the FaceColor property to 'texturemap'. You might also want to set the EdgeColor to 'off'
Thanks for the answer...Can you give a practical example please?
mesh(A,'FaceColor','texturemap','EdgeColor','none')
But I'm not sure that's what you're referring to. Can you explain what you'd like to see? Something more like this, perhaps?
bar3(A)

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!