現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Categorising the star into different types
2 ビュー (過去 30 日間)
古いコメントを表示
Lorenne
2018 年 5 月 1 日
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]
採用された回答
Ameer Hamza
2018 年 5 月 1 日
Use this to get the color matrix
index = mat2cell(name == type, ones(length(name), 1), 4);
colorNum = cellfun(@find, index);
myMatrix = color(colorNum, :);
22 件のコメント
Lorenne
2018 年 5 月 1 日
Can i use the logical method eg: because first element is 'K', so star_index = [0 0 0 1]
Lorenne
2018 年 5 月 1 日
and from that i know the 5th column correspond to the 5th row of colour matrix but how to plot it
Ameer Hamza
2018 年 5 月 1 日
name == type
will give you a logical matrix corresponding to type in each row.
Lorenne
2018 年 5 月 1 日
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
2018 年 5 月 1 日
The myMatrix in my code contain all the colors corresponding to each row.
Lorenne
2018 年 5 月 1 日
I have to plot the values with the corresponding colour eg: the first element with the colour[1,0.81,0.59] onto the original graph which also contain the data of the first element
Ameer Hamza
2018 年 5 月 1 日
Please show an example of the graph. Your question does not contain any data, which can be used to plot using the calculated color.
Guillaume
2018 年 5 月 1 日
index = mat2cell(name == type, ones(length(name), 1), 4);
would be simpler as
index = num2cell(name == type, 2);
But using ismember would avoid all this needless transition through cell array.
Ameer Hamza
2018 年 5 月 1 日
@Guillaume, thanks for sharing a compact alternative. ismember is an even better alternative as you have described in your answer.
Ameer Hamza
2018 年 5 月 2 日
How is the name and style variable linked to the above graph? How will you decide which point will have which color?
Lorenne
2018 年 5 月 3 日
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
2018 年 5 月 3 日
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
2018 年 5 月 3 日
編集済み: Ameer Hamza
2018 年 5 月 3 日
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
2018 年 5 月 3 日
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
2018 年 5 月 4 日
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
2018 年 5 月 4 日
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
2018 年 5 月 4 日
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).
その他の回答 (1 件)
Guillaume
2018 年 5 月 1 日
編集済み: Guillaume
2018 年 5 月 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 件のコメント
Guillaume
2018 年 5 月 1 日
編集済み: Guillaume
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.
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 日
The actual inputs are quite long, but one of the element contain 'U' which is not in the type string
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?
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 日
It's still not coloured, the colour matrix is correct now for all values but how do i plot it in the graph?
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 日
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]
Lorenne
2018 年 5 月 4 日
Not scatter cause the original graph was using plot? And sorry the point with ‘U’ which is not belong to any colour type has to be uncoloured
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 日
Uncoloured means removing the points from the original graph so there will be no more black colour in this case
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
参考
カテゴリ
Help Center および File Exchange で Annotations についてさらに検索
タグ
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)