Compare arrays to find common non zero indexes

조회 수: 1 (최근 30일)
lucksBi
lucksBi 2017년 8월 4일
댓글: lucksBi 2017년 8월 6일
Hey I have one 2d and one cell array like this:
A=[14, 1, 0,15;
1, 2, 4, 0;
3, 0, 0, 0;
14, 5, 0, 0;
0,12, 4, 0]
x={[2,3,4,5];[1,3,4,5];[1,2,4,5]}
I want to find common non-zero elements in A based on X.
For example, firstly x{1,1}=[2,3,4,5] will be compared with A. As it is x{1,1} so all elements of x will be compared with 1st row in A. (first comparison will be row 1 and row 2 in A and common non zero index are 1 & 2 then next row 1 and row 3 and common non-zero index is 1 and similarly row 4 and row 5 will be compared) Then in same way for x{2,1} comparison will be between row 2 and rows 1,3,4 and 5 of A.
Thanks in advance
  댓글 수: 2
Jan
Jan 2017년 8월 4일
편집: Jan 2017년 8월 4일
What have you tried so far? Which problems occurred? What is the wanted output for this example?
I do not understand:
(first comparison will be row 1 and row 2 in A and common non zero
index is 2 then next row 1 and row 3 and common non-zero indexes are
1 and 2 and similarly row 4 and row 5 will be compared)
lucksBi
lucksBi 2017년 8월 4일
I have edited my question.. Sorry for confusion

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2017년 8월 4일
편집: Jan 2017년 8월 5일
A=[14, 1, 0,15;
1, 2, 4, 0;
3, 0, 0, 0;
14, 5, 0, 0;
0,12, 4, 0];
x = {[2,3,4,5]; [1,3,4,5]; [1,2,4,5]};
result = cell(size(x));
for k = 1:numel(x)
Ak = A(k, :); % Specified row
xk = x{k};
D = cell(1, numel(xk));
for r = 1:numel(xk)
D{r} = find(Ak & A(xk(r), :)); % Or: Ak .* A(xk(r), :)
end
result{ix} = D;
end
UNTESTED
  댓글 수: 3
Jan
Jan 2017년 8월 5일
I've edited the answer. Does it create the wanted output?
lucksBi
lucksBi 2017년 8월 6일
Yes thanks alot for helping.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by