필터 지우기
필터 지우기

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 CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by