How to convert distinct strings in a cell array to numbers.
조회 수: 10 (최근 30일)
이전 댓글 표시
I am trying to design a function that will that will rename each element in a cell array a unique number. For example, I am starting with this array of strings:
'Loc' 'Buf'
'Cac' 'Loc'
'Cac' 'Buf'
'Buf' 'D'
And want to rename each a number, so it becomes:
1 2
3 1
3 2
2 4
I.e., I need a function that will make 'Loc'=1, 'Buf'=2, etc.
댓글 수: 0
답변 (2개)
Titus Edelhofer
2015년 1월 28일
Hi,
something like this:
x = {'Loc' 'Buf'
'Cac' 'Loc'
'Cac' 'Buf'
'Buf' 'D'}
[C,~,ib] = unique(x);
% the unique strings:
C
% and the corresponding id's:
ib = reshape(ib, size(x))
Titus
댓글 수: 0
wgourlay
2018년 7월 30일
You can also ensure the numbering is in order of occurrence (this was necessary in my case):
[C,~,ib] = unique(x, 'stable');
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!