Match numbers to allocate text

1 visualización (últimos 30 días)
SChow
SChow el 8 de Ag. de 2022
Respondida: David Hill el 8 de Ag. de 2022
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

Respuesta aceptada

Karim
Karim el 8 de Ag. de 2022
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

Más respuestas (1)

David Hill
David Hill el 8 de Ag. de 2022
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'

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by