How to compare rows in a matrix

조회 수: 65 (최근 30일)
Sameer
Sameer 2014년 7월 18일
댓글: Sameer 2014년 7월 18일
Hello all
I have a matrix A
A =
1 1 0 0 0
0 -1 0 1 0
-1 0 1 1 0
lets say I am looking at the second row in which column 2 and 4 have value present now if I compare it with the row above which has values in the column 1 and column 2 so I want to know the column number which has values in both the rows, I mean here the column number 2 should be returned. Similarly row 3 should be compared with row 1 and 2 and for each row a column number should be returned.
Can anyone please guide me through this.
Regards
  댓글 수: 1
Azzi Abdelmalek
Azzi Abdelmalek 2014년 7월 18일
편집: Azzi Abdelmalek 2014년 7월 18일
What is the expected result for your example?

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 7월 18일
Comparison between row 1 and row 2
A =[ 1 1 0 0 0
0 -1 0 1 0
-1 0 1 1 0]
idx=find(all(A(1:2,:)))
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2014년 7월 18일
A =[ 1 1 0 0 0
0 -1 0 1 0
-1 0 1 1 0]
jj=0;
for k=2:size(A,1)
for p=1:k-1
B=A([p,k],:)
jj=jj+1;
out{jj,2}=find(all(B))
out{jj,1}=sprintf('(%d,%d)',k,p)
end
end
Sameer
Sameer 2014년 7월 18일
Thanks a lot

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

추가 답변 (1개)

the cyclist
the cyclist 2014년 7월 18일
I think that
[rows,cols] = find(A(2:end,:)~=0 & A(1:end-1,:)~=0)
does what you want.
rows will be the index to the "top" row being compared, and "cols" will be the column numbers that meet your criterion.
For example, in your case
rows = [1; 2];
and
cols = [2; 4];
cols seems to be the main thing you are going for.
  댓글 수: 2
the cyclist
the cyclist 2014년 7월 18일
My solution only compares neighboring pairs of rows.
Sameer
Sameer 2014년 7월 18일
Thanks a lot

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by