Categorising the star into different types
显示 更早的评论
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]
采纳的回答
更多回答(1 个)
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 个评论
Lorenne
2018-5-1
name=['K';'K';'G';'A';'B']
and
name = 'KKGAB'.'
will result in exactly the same array. A 5x1 char array indeed. My way is more concise.
Have you actually tried my answer? It is a lot simpler and a lot faster than needlessly converting to a cell array and using cellfun and find.
Lorenne
2018-5-1
Guillaume
2018-5-1
You'll get that error with either method when a character in name is not present in type.
Either that or the inputs are not as you described. Best would be to show us the actual inputs.
Lorenne
2018-5-2
Lorenne
2018-5-2
Guillaume
2018-5-2
The actual inputs are quite long
You can always attach a mat file to your question. Or shorten it a bit.
Your screenshot shows an excel file (you could attach that), which if imported properly into matlab would result in a cell array of char arrays or a string array, not a char column vector as in your example. The solution for cell arrays or string arrays would be the same but different than that of the char column vector. So please clarify what the true inputs are.
one of the element contain 'U' which is not in the type string
What do you want to do in that case then?
Lorenne
2018-5-2
Guillaume
2018-5-2
That 'U' will be remained black colour
In that case,
colour_with_black = [0, 0, 0; colour];
[~, idx] = ismember(name, type);
namecolour = colour_with_black(idx + 1, :)
the second return value of ismember is 0 when the name is not found in type. Adding 1 to that makes it a valid index in the new colour table, with black as the first index. All the other indices are shifted by one, same as in the colour table.
Lorenne
2018-5-3
Lorenne
2018-5-3
Lorenne
2018-5-3
Guillaume
2018-5-3
It's still not coloured
What is "it"? Was does plotting a colour matrix mean? It looks like you want a scatter plot but you've never mentioned anything about coordinates. We can't read your mind. Looking at your snippet of code, I'm going to hazard a guess:
%...
namecolour = colour_with_black(idx + 1, :);
scatter(star_rad, star_temp, 36, namecolour);
set(gca, 'XScale', 'log');
set(gca, 'YScale', 'log');
what's the '~' for?
Lorenne
2018-5-3
Guillaume
2018-5-3
So, doesn't the scatter I've shown do what you want?
Lorenne
2018-5-4
Guillaume
2018-5-4
Not scatter cause the original graph was using plot?
I don't understand what you mean. Using plot to plot each point individually is extremely inefficient. scatter will do the same all in one go. Either way, you can have plot and scatter in the same graph so I don't see what the issue is.
What does uncoloured mean. You keep moving the goal post. We started with needing the generate a colour matrix, then needing to take into account elements not present in name, then needing to plot these elements in black, now they need to be uncoloured.
Lorenne
2018-5-5
Guillaume
2018-5-6
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
类别
在 帮助中心 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




