WHat is this : uint8((dou​ble(weathe​r1618) ./ maxv) .* 50); ?

1 view (last 30 days)
This code (almost) does what I want but I don't understand how it can be that simple. So can someone, please, explain me how this code works?
FIY, weather1618 is a 384x384 array with a range from -76 to -30. And maxv has a value of -30.
mapped_array = uint8((double(weather1618) ./ maxv) .* 50);
image(mapped_array);
If I was to do directly,
image(weather1618);
I would only get a blue image.

Answers (2)

Walter Roberson
Walter Roberson on 7 May 2017
When data in the range -76 to -30 is divided by -30, the result will be in the range -76/-30 to -30/-30, which is approximately 2 1/2 to 1. Multiply by 50 to get approximately the range 127 to 50. The uint8() would make that 50 exactly to 127 exactly.
By default when passed uint8 data, image() uses CDataMapping 'direct'. That causes value 0 to map to the first color in the color map, value 1 to map to the second, and so on; if any values in the data are greater than or equal to the length of the color map, then they are mapped to the last color.
Thus, provided that your colormap is large enough, the data would be mapped into entries #51 to 128.
But is your colormap large enough? By default colormap uses 64 colors. Perhaps something else in your code used colormap() with a different number of colors or a specific colormap. If not, if the default was used, then the entries #51 to #64 would get used and everything else would use #64.
If that were the case, then effectively the values near -30 would map to individual colors, but the values below -38.4 would map to the last color in the colormap.
Now overall, a better way to handle all of this would be to extract the portion of the colormap that you wanted to use, flip it top to bottom, colormap() that, image(weather1618) and caxis([-38.4 -30]) . The way it is being done now can certainly work, but it is not transparent as to what it is doing -- selecting a portion of the colormap and mapping a particular range (backwards) to the selected colors.

Image Analyst
Image Analyst on 31 May 2017
Try
imshow(weather1618, []);
or, if you (for some reason) want an array in the range 0-50:
mapped_array = uint8(50 * mat2gray(weather1618));
imshow(mapped_array);

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!