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 Kommentar

Azzi Abdelmalek
Azzi Abdelmalek am 18 Jul. 2014
Bearbeitet: Azzi Abdelmalek am 18 Jul. 2014
What is the expected result for your example?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 18 Jul. 2014

0 Stimmen

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 Kommentare

Sameer
Sameer am 18 Jul. 2014
Hi thanks for replying. The answer for second row must be 2 which is correct as per the suggested approach but for the third row answer should be 1 and 4 as when we consider 3 row then it should be compared to 1st row and gives me 1 and then with second row andgive me 4 so theanswer for 3rd row should be 1 and 4.
Regards
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 am 18 Jul. 2014
Thanks a lot

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

the cyclist
the cyclist am 18 Jul. 2014

1 Stimme

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 Kommentare

the cyclist
the cyclist am 18 Jul. 2014
My solution only compares neighboring pairs of rows.
Sameer
Sameer am 18 Jul. 2014
Thanks a lot

Melden Sie sich an, um zu kommentieren.

Kategorien

Tags

Gefragt:

am 18 Jul. 2014

Kommentiert:

am 18 Jul. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by