How to generate colormap for specific matrix?

8 views (last 30 days)
I have matrix with values and corresponding index number or class number. I want to generate a colormap based on the values of the matrix not the on index number. I have attached a file, where first column indicates values and 2nd column is class number. I am able to do it based on the class number. But, I want to generate a colomap based on the values provided in 1st column in myfile. Means if these values [ 315.2184 315.828 323.7528 323.9052 338.3832 ] are present in an array it will be red color and so on.
Please suggest me as I am unable to do that. Thank you.

Accepted Answer

Dave B
Dave B on 29 Jul 2021
Edited: Dave B on 29 Jul 2021
I'm not 100% sure I understand the quesiton but here's how I interpreted it:
There's a map that goes from a set of raw values to one of four numbers, and we want to apply that map to a separate set of raw values.
Here's some test data to simulate what's in your excel file:
vals = rand(10,1); % 10 values
classes = randi(4,10,1); % each value is one of four random classes
Here's an example 25x25 image to be mapped:
rawimage=vals(randi(10,25,25))
Here's rawimage converted to classes using the map. Note that using 'nearest' means that if there was something in vals that's not in classes, but in between two vals that are in classes, it'll use the nearest class:
mappedimage=interp1(vals, classes, rawimage, 'nearest')
Call image, and set an appropriate colormap to view. Note that image will use CDataMapping='direct' by default, which means 1 corresponds to the first value in the colormap, 2 to the second value, etc.
image(mappedimage)
colormap lines
Alternatively, to get the color values and not display them:
b=ind2rgb(mappedimage,lines(4));
You might also check out imquantize in the image processing toolbox, it let's you input the boundaries between categories and will do the rest for you.

More Answers (0)

Categories

Find more on Colormaps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!