Looking for help with labelling groups

조회 수: 5 (최근 30일)
Vivien Gerwing
Vivien Gerwing 2024년 9월 12일
댓글: Vivien Gerwing 2024년 9월 12일
Hey everyone,
so far I've read all Matlab entrys on adding/creating categories and/or labels but I haven't found something fitting that works for me. I have a set of data in 178x13 double format, the variable names in a 1x13 cell format and the group classification in a 178x1 double, which ist just 1 2 and 3. In different projects I had a 178x1 cell instead containing the names of the groups and I would use that to generate a double using findgroups. However, now I would like to do the reverse action and generate a 178x1 cell value with the names instead of numbers. Num2Cell does not work here of course because I would like to tell Matlab to put "GroupA" for every 1 in the group classification 178x1 double and "GroupB" for every 2 and GroupC for every 3. I have information that the first 59 belog to GroupA etc, but I don't know how to implement that either. Maybe it's the language barrier but I really don't even know what to search for anymore. Any help is much appreciated!
  댓글 수: 1
the cyclist
the cyclist 2024년 9월 12일
Can you upload the data (or a small representative sample)? You can use the paper clip icon in the INSERT section of the toolbar.

댓글을 달려면 로그인하십시오.

채택된 답변

Drew
Drew 2024년 9월 12일
편집: Drew 2024년 9월 12일
Here is a solution:
% Your group classification vector
% This is a 7x1 double for an example.
% Yours would be 178x1 double
groupClassification = [1; 1; 2; 3; 2; 1; 3];
% Initialize a cell array for group names
groupNames = cell(size(groupClassification));
% Define the mapping from numbers to group names
groupNamesMap = {'GroupA', 'GroupB', 'GroupC'};
% Loop through each group classification and assign the corresponding name
for i = 1:length(groupClassification)
groupNames{i} = groupNamesMap{groupClassification(i)};
end
% Display the result
groupNames
groupNames = 7x1 cell array
{'GroupA'} {'GroupA'} {'GroupB'} {'GroupC'} {'GroupB'} {'GroupA'} {'GroupC'}
Or, alternately, using logical indexing:
groupClassification = [1; 1; 2; 3; 2; 1; 3];
% Or, use logical indexing
% Pre-allocate the cell array
groupNames = cell(size(groupClassification));
% Assign group names using logical indexing
groupNames(groupClassification == 1) = {'GroupA'};
groupNames(groupClassification == 2) = {'GroupB'};
groupNames(groupClassification == 3) = {'GroupC'};
% Display the result
groupNames
groupNames = 7x1 cell array
{'GroupA'} {'GroupA'} {'GroupB'} {'GroupC'} {'GroupB'} {'GroupA'} {'GroupC'}
If this answer helps you, please remember to accept the answer.
  댓글 수: 1
Vivien Gerwing
Vivien Gerwing 2024년 9월 12일
Thank you so much! I got so close with my idea but I messed up at the step with the loop. Much appreciated and now everything looks so much better!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by