Categorising the star into different types

3 Ansichten (letzte 30 Tage)
Lorenne
Lorenne am 1 Mai 2018
Kommentiert: Guillaume am 6 Mai 2018
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]

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 1 Mai 2018
Use this to get the color matrix
index = mat2cell(name == type, ones(length(name), 1), 4);
colorNum = cellfun(@find, index);
myMatrix = color(colorNum, :);
  22 Kommentare
Ameer Hamza
Ameer Hamza am 4 Mai 2018
Why not add another element 'U' to the type='ABGK' vector like this
type='ABGKU'
and corrosponding color matrix will become like this
[0.64,0.73,1;
0.7,0.88,0.89;
1,1,0.6;
1,0.81,0.59;
0,0,0];
It will cause 'U' element to have [0 0 0] color which is black.
Guillaume
Guillaume am 4 Mai 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).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Guillaume
Guillaume am 1 Mai 2018
Bearbeitet: Guillaume am 1 Mai 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.
  19 Kommentare
Lorenne
Lorenne am 5 Mai 2018
Uncoloured means removing the points from the original graph so there will be no more black colour in this case
Guillaume
Guillaume am 6 Mai 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

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by