You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Categorising the star into different types
1 view (last 30 days)
Show older comments
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
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, :);
22 Comments
Ameer Hamza
on 1 May 2018
name == type
will give you a logical matrix corresponding to type in each row.
Lorenne
on 1 May 2018
Yeah, i did that, and it gives me a matrix where if the first element is K, then the ans will be [0 0 0 1] ,but how do i relate this to the colour matrix which the 4th row, [1,0.81,0.59] is the K-type colour , and plot this to the graph?
Ameer Hamza
on 1 May 2018
The myMatrix in my code contain all the colors corresponding to each row.
Ameer Hamza
on 1 May 2018
Please show an example of the graph. Your question does not contain any data, which can be used to plot using the calculated color.
Ameer Hamza
on 1 May 2018
@Guillaume, thanks for sharing a compact alternative. ismember is an even better alternative as you have described in your answer.
Ameer Hamza
on 2 May 2018
How is the name and style variable linked to the above graph? How will you decide which point will have which color?
Lorenne
on 3 May 2018
its to another vector x and y, where the position of x correspond to the 'name' variable.Eg: x = [1 ; 2 ; 3 ; 4 ; 5] y = [2 ; 6 ; 5 ; 8 ; 1] name = ['K';'K';'G';'A';'B'] then that (1,2) point will be the 'K' so it will be coloured [1,0.81,0.59]
Ameer Hamza
on 3 May 2018
You can draw the points with corresponding colors like this.
x = [1 ; 2 ; 3 ; 4 ; 5];
y = [2 ; 6 ; 5 ; 8 ; 1];
ax = gca;
for i=1:length(x)
line(x(i), y(i), 'Marker', 'o', 'Color', myMatrix(i, :));
end
Ameer Hamza
on 3 May 2018
Edited: Ameer Hamza
on 3 May 2018
gca is used to access the handle of current axes. Or if no axes are present, it will create a new axis and return its handle.
Ameer Hamza
on 3 May 2018
The easiest way is to treat row 14 as a special case. Remove it from the original matrix and then plot it manually at the end.
Lorenne
on 4 May 2018
But there’s a lot of it in the matrix and it should be uncoloured sorry .. so at first all x and y values are black and we have to colour it based on the name it has based on the colour matrix and the one with ‘U’ has to be uncoloured means remove the black colour
Ameer Hamza
on 4 May 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
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).
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.
19 Comments
Guillaume
on 1 May 2018
Edited: Guillaume
on 1 May 2018
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.
Guillaume
on 2 May 2018
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?
Guillaume
on 2 May 2018
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.
Guillaume
on 3 May 2018
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
on 3 May 2018
It's like x = [1;0;9;2;4] y = [5;10;12;4;9] and name = ['A';'K';'K';'G';'B'] type = 'ABGK' colour =A-type coloured[0.64,0.73,1] B-type coloured[0.7,0.88,0.89] G-type coloured[1, 1, 0.6] K-type coloured[1,0.81,0.59] where the x and y value corresponds to the name, eg: for the point where x=9, y=12,its name is 'K' so it needs to be coloured [1,0.81,0.59]
Guillaume
on 4 May 2018
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.
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
See Also
Categories
Find more on Performance and Memory in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)