필터 지우기
필터 지우기

Recode strings in Cell array

조회 수: 1 (최근 30일)
Gmc
Gmc 2015년 1월 16일
댓글: Gmc 2015년 1월 26일
I have a cell array of strings like this:
codes =
'Home' '5'
'Home' '1'
'Home' '2'
'Home' '1'
'Home' '1'
'Home' '5'
'Home' '1'
'Home' '1'
'Home' '1'
'Home' '5'
'Base' '1'
'Base' '1'
'Base' '1'
'Base' '1'
'Base' '6'
'Base' '1'
'Base' '1'
'Base' '1'
'Base' '1'
'Base' '1'
I need to recode the data such that if codes(1,1) is Home and codes(1,2) is 5 then print 'Parent'. Or if codes(5,1) is Base and codes(5,2) is 6 then print "Child" and so on. Ideally these new codes would go to a new vector. I've tried everything I can think of and am pretty stuck. I thought I could just write a lengthy if statement but I get the eq error for using something like if codes(:,1) == 'Home' Please help! Thank you - gmc

채택된 답변

Shoaibur Rahman
Shoaibur Rahman 2015년 1월 17일
indx = 0;
for k = 1:size(codes,1)
if strcmp(codes(k,1),'Home') && strcmp(codes(k,2),'5')
indx = indx+1;
NewVector{indx,:} = [codes(k,1) codes(k,2)];
disp('Parent')
elseif strcmp(codes(k,1),'Base') && strcmp(codes(k,2),'6')
indx = indx+1;
NewVector{indx,:} = [codes(k,1) codes(k,2)];
disp('Child')
end
end
If you want ParentVector and ChildVector separately, then define two different indices.
  댓글 수: 1
Gmc
Gmc 2015년 1월 26일
Cheers! This worked great!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by