Filter löschen
Filter löschen

Recode strings in Cell array

1 Ansicht (letzte 30 Tage)
Gmc
Gmc am 16 Jan. 2015
Kommentiert: Gmc am 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

Akzeptierte Antwort

Shoaibur Rahman
Shoaibur Rahman am 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.
  1 Kommentar
Gmc
Gmc am 26 Jan. 2015
Cheers! This worked great!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by