フィルターのクリア

How to convert distinct strings in a cell array to numbers.

10 ビュー (過去 30 日間)
Joel Mueller
Joel Mueller 2015 年 1 月 28 日
回答済み: wgourlay 2018 年 7 月 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.

回答 (2 件)

Titus Edelhofer
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

wgourlay
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');

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by