MATLAB: Recode strings in Cell array

cell arraysstrings

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

Best Answer

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.
Related Question