Categorising the star into different types

3 views (last 30 days)
Let's say i have a column vector string, name=['K';'K';'G';'A';'B'] and i have a matrix , type='ABGK' I have another matrix index=(rows,columns) where index=(length(name),length(type)) and a colour matrix=[0.64,0.73,1;0.7,0.88,0.89;1,1,0.6;1,0.81,0.59],where the 1st row corresponds to the first type(A-type) and so on. How do I categorise the 'name' into its type 'type' and then it will correspond to the colour matrix so that the first element in name ='K' will be categorised into 'K-type' ,and its colour will be the 4th row of the colour matrix=1,0.81,0.59 , so that when i plot it will give the colour[1,0.81,0.59]

Accepted Answer

Ameer Hamza
Ameer Hamza on 1 May 2018
Use this to get the color matrix
index = mat2cell(name == type, ones(length(name), 1), 4);
colorNum = cellfun(@find, index);
myMatrix = color(colorNum, :);
Ameer Hamza
Ameer Hamza on 4 May 2018
Why not add another element 'U' to the type='ABGK' vector like this
and corrosponding color matrix will become like this
It will cause 'U' element to have [0 0 0] color which is black.
Guillaume on 4 May 2018
Really, I don't understand why this conversation about U is still going on. This has been solved days ago in a very straightforward manner:
colour_with_black = [0, 0, 0; colour];
[~, idx] = ismember(name, type);
namecolour = colour_with_black(idx + 1, :)
The method in which you plot the data is completely independent of how you generate the colour matrix (and really should be another question).

Sign in to comment.

More Answers (1)

Guillaume on 1 May 2018
Edited: Guillaume on 1 May 2018
Use the 2nd return value of ismember as row index into your colour matrix:
name = 'KKGAB'.';
type = 'ABGK';
colour = [0.64,0.73,1;0.7,0.88,0.89;1,1,0.6;1,0.81,0.59];
[~, idx] = ismember(name, type);
namecolour = colour(idx, :)
Note: the shape of namecolour will be a column vector regardless of the shape of name. linear indices of both will match however.
Lorenne on 5 May 2018
Uncoloured means removing the points from the original graph so there will be no more black colour in this case
Guillaume on 6 May 2018
Well, now we're really moving the goalpost. After asking that the points be black, now they have to be removed. Why didn't you ask that in the first place? The procedure is completely different.
[isfound, idx] = ismember(name, type);
namecolour = colour(idx(isfound), :); %only get colour for names found in type
scatter(star_rad(isfound), star_temp(isfound), 36, namecolour); %only plot the points that were found in type

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!