compare elements of cell and 2D array

2 ビュー (過去 30 日間)
lucksBi
lucksBi 2017 年 4 月 4 日
コメント済み: lucksBi 2017 年 4 月 4 日
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
ranjith kumar reddy P 2017 年 4 月 4 日
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 Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by