How to replace elements in a integer vector with chars using a lookup table

조회 수: 1 (최근 30일)
I have a vector of unsigned integer (A) Nx1 uint8 (N>50000) populated with values from 0 to 6.
On the other hand there is a lookup table (B) 7x2 table, like this:
STATE CODE
----- ----
'Str 1' 0
'Str 2' 1
... ...
'Str 7' 6
class(B.STATE) --> cell
class(B.CODE) --> double
Question: How can I obtain a vector of strings (C) with a size Nx1, with each element being the match in the lookup table?
Example: A(i) = 6 --> C(i) = 'Str 7'
Thanks for your support.
E.

채택된 답변

Akira Agata
Akira Agata 2020년 1월 23일
How about the following way?
% Sample data
A = randi([0 6],10,1);
B = table(compose('Str %d',1:7)',(0:6)',...
'VariableNames',{'STATE','CODE'});
% Create the output array
[~,loc] = ismember(A,B.CODE);
C = B.STATE(loc);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by