I have the cell array:
im = {'A+','B-','B+','A+'; 'A-','B-', NaN, 'A+'};
I want to loop through each row to see which ones that have both sums of 'A's and 'B's equal to 2. Then i would replace these rows with NaNs. I have the code:
for ii = 1: size(im,1)
if (sum(strcmp('A+', im), 2) + sum(strcmp('A-', im), 2)) == 2 & (sum(strcmp('B+', im), 2) + sum(strcmp('B-', im), 2))== 2
im{ii, 1} = NaN;
im{ii, 2} = NaN;
im{ii, 3} = NaN;
im{ii, 4} = NaN;
end
end
When i remove the 2nd row from im
, i get the ans: im = {NaN, NaN, NaN, NaN}
. However, when i include the 2nd row, im
remains as it is originally. Please, what could possibly be wrong with my code? Secondly, is there a better way to replace the elements?
Any help or suggestions please??? Many thanks.
Your code doesn't work because in the if
statement you are checking for the sum in the whole cell, not just in a single row. You should use something like this:
if (sum(strcmp('A+', im(ii,:)), 2) + sum(strcmp('A-', im(ii,:)), 2)) == 2 & (sum(strcmp('B+', im(ii,:)), 2) + sum(strcmp('B-', im(ii,:)), 2))== 2
Additionaly, you can change your next four lines for a single one like this:
im(ii,:) = {NaN}