Recode strings in Cell array

1 view (last 30 days)
Gmc
Gmc on 16 Jan 2015
Commented: Gmc on 26 Jan 2015
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

Accepted Answer

Shoaibur Rahman
Shoaibur Rahman on 17 Jan 2015
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.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!