convert values to string
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a vector with values to define groups (groupValue, in this case 1000x1 array with values from 1 to 5) I also have a cell array with strings for each of these values (labels, 5x1 cell array) I want to have a 1000x1 cell array with strings for the labels instead of values. How can I do this more efficiently i.e. without a for loop?
for kk = 1:nElements
groupLabels{kk} = labels{groupValue(kk)};
end
댓글 수: 0
채택된 답변
Guillaume
2015년 12월 21일
Simply,
groupLabels = labels(groupValue);
댓글 수: 2
Guillaume
2015년 12월 21일
Yes, for this to work groupValue must be integers between 1 and numel(labels). You could precede the above line with:
validateattributes(groupValue, {'numeric'}, {'positive', 'integer', '<=', numel(labels)}, '', 'groupValue'};
추가 답변 (2개)
Geoff Hayes
2015년 12월 21일
Ingrid - if labels is your cell array of string labels and groupValue is your integer array of group values (or indices into labels) then perhaps you could try something like
groupLabels = labels(groupValue);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!