Recode strings in Cell array
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Argument Definitions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!