convert values to string
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ingrid
am 21 Dez. 2015
Kommentiert: Guillaume
am 21 Dez. 2015
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 Kommentare
Akzeptierte Antwort
Guillaume
am 21 Dez. 2015
Simply,
groupLabels = labels(groupValue);
2 Kommentare
Guillaume
am 21 Dez. 2015
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'};
Weitere Antworten (2)
Geoff Hayes
am 21 Dez. 2015
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 Kommentare
Renato Agurto
am 21 Dez. 2015
hello. This should work
groupLabels = labels(groupValue);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!