Match numbers to allocate text

1 回表示 (過去 30 日間)
SChow
SChow 2022 年 8 月 8 日
回答済み: David Hill 2022 年 8 月 8 日
Hi,
I am struggling to match numbers to assign a corresponding string. For example,
I have an array
x=[A 12
B 13
C 19
D 21]
and another array
y= [12 13 19 24 21 12 12]
I would prefer to get
z=[12 A
13 B
19 C
24 NaN
21 D
12 A
12 A]
Any help is appreciated

採用された回答

Karim
Karim 2022 年 8 月 8 日
you can use the categorical command to do this:
y = [12 13 19 24 21 12 12]';
B = categorical( y, [12 13 19 21], ["A" "B" "C" "D"] )
B = 7×1 categorical array
A B C <undefined> D A A

その他の回答 (1 件)

David Hill
David Hill 2022 年 8 月 8 日
Your could also use a sparse array or an array of nan.
x=[12 13 19 21];
y=[12 13 19 24 21 12 12];
X=nan(1,25);
X(x)='ABCD';
Y=char(X(y))
Y = 'ABC DAA'
x=[12 13 19 21];
y=[12 13 19 24 21 12 12];
X=sparse(1,25);
X(x)='ABCD';
Y=char(full(X(y)))
Y = 'ABC DAA'

カテゴリ

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