replace number by string

조회 수: 4 (최근 30일)
Mohammed Kamruzzaman
Mohammed Kamruzzaman 2015년 4월 9일
답변: Jos (10584) 2015년 4월 10일
I have a vector with numeric values and I would like to replace the number in a given cell by a string (data are attached). For instance, 23=BDHE, 9997=BEIS, 11=CUAL, 4=CTGB, 60=MYMU, 1010=INJP, 3006=YNTA, and 80=TRQO. Thanks in Advance.

채택된 답변

Mohammad Abouali
Mohammad Abouali 2015년 4월 10일
Well, a quick solution would be:
valueCell={ 23,23,23, ...
9997,9997,9997,9997,9997,9997,9997, ...
11,11,11,11,11,11,11, ...
4,4,4,4,4, ...
60,60,60,60,60,60, ...
1010,1010,1010,1010,1010,1010,1010,1010, ...
3006,3006,3006,3006,3006, ...
80,80,80,80,80};
strCell=cell(size(valueCell));
strCell( cellfun(@(c) c==23,valueCell) )={'BDHE'};
strCell( cellfun(@(c) c==9997,valueCell) )={'BEIS'};
strCell( cellfun(@(c) c==11,valueCell) )={'CUAL'};
strCell( cellfun(@(c) c==4,valueCell))={'CTGB'};
strCell( cellfun(@(c) c==60,valueCell) )={'MYMU'};
strCell( cellfun(@(c) c==1010,valueCell) )={'INJP'};
strCell( cellfun(@(c) c==3006,valueCell) )={'YNTA'};
strCell( cellfun(@(c) c==80,valueCell) )={'TRQO'};
But a better solution would be
valueCell={ 23,23,23, ...
9997,9997,9997,9997,9997,9997,9997, ...
11,11,11,11,11,11,11, ...
4,4,4,4,4, ...
60,60,60,60,60,60, ...
1010,1010,1010,1010,1010,1010,1010,1010, ...
3006,3006,3006,3006,3006, ...
80,80,80,80,80};
valueTable=cell2table(valueCell','VariableNames',{'value'});
conversionTable=table([ 23, 9997, 11, 4, 60, 1010, 3006, 80]', ...
{'BDHE','BEIS','CUAL','CTGB','MYMU','INJP','YNTA','TRQO'}', ...
'VariableNames',{'value','str'});
strTable=join(valueTable,conversionTable);
  댓글 수: 1
Mohammed Kamruzzaman
Mohammed Kamruzzaman 2015년 4월 10일
Thank you so much. Your code is perfect.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jos (10584)
Jos (10584) 2015년 4월 10일
% conversion rules V(k) corresponds to S(k):
V = [ 3 4 6 9 1] ;
S = {'AA','B','CCC','D','EEE'} ;
ValuesIn = [3 6 4 6 3 1 9 9 0 4 4 1 3] % note the 0!
% engine to convert ValuesIn to StrOut
StrOut = repmat({'Unknown'},size(ValuesIn)) ;
[tf, idx] =ismember(ValuesIn, V) ;
StrOut(tf) = S(idx(tf))

카테고리

Help CenterFile Exchange에서 Vector Volume Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by