이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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?
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!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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 (한국어)