Match numbers to allocate text
3 次查看(过去 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
0 个评论
采纳的回答
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"] )
0 个评论
更多回答(1 个)
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))
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)))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!