Match numbers to allocate text

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by