compare elements of cell and 2D array

1 次查看(过去 30 天)
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.
Thanks in advance.

采纳的回答

ranjith kumar reddy P
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
  1 个评论
lucksBi
lucksBi 2017-4-4
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by