Effacer les filtres
Effacer les filtres

Match numbers to allocate text

5 vues (au cours des 30 derniers jours)
SChow
SChow le 8 Août 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

Réponse acceptée

Karim
Karim le 8 Août 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

Plus de réponses (1)

David Hill
David Hill le 8 Août 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'

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by