compare elements of cell and 2D array

2 views (last 30 days)
lucksBi on 4 Apr 2017
Commented: lucksBi on 4 Apr 2017
Hey I have a 2D array like
a=[0 0 3 4 0 0; 1 0 3 4 0 0]
and a cell array like
b(1,1)=[0 2 0 0 0 0; 1 2 3 4 0 0]
b(2,1)=[0 0 3 4 0 0; 0 2 0 0 0 0; 1 2 3 4 0 0]
For each non zero element in each row of a, it should search the element in corresponding cell array and display row index at where it is present. e.g for 3 in first row, it will search in b(1,1) and display 2 (as 3 is present at 2nd row) and e.g. for 4 in 2nd row it will search in b(2,1) and display 1 and 3.

ranjith kumar reddy P on 4 Apr 2017
a=[0 0 3 4 0 0; 1 0 3 4 0 0];
b{1,1}=[0 2 0 0 0 0; 1 2 3 4 0 0];
b{2,1}=[0 0 3 4 0 0; 0 2 0 0 0 0; 1 2 3 4 0 0];
NZelem = find(a);
for i = 1:length(NZelem)
c = a(NZelem(i));
d1 = find(b{1,1}==c);
r1 = rem(d1,2);
if(r1==0)
printf('the element %d in b{1,1} is in row %d',c,2);
else
printf('the element %d in b{1,1} is in row %d',c,1);
end
d2 = find(b{2,1}==c);
r2 = rem(d2,2);
if(r2==0)
printf('the element %d in b{2,1} is in row %d',c,2);
else
printf('the element %d in b{2,1} is in row %d',c,1);
end
end
lucksBi on 4 Apr 2017
Thanks Alot for your help. But b is a cell array. and b{1,1} & b{2,1} are elements of b. and also b is a large array of almost 50 elements like this. Is there a way to do it without separately accessing each element of b.