# How to compare rows in a matrix

77 visualizzazioni (ultimi 30 giorni)
Sameer il 18 Lug 2014
Commentato: Sameer il 18 Lug 2014
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 CommentoMostra NessunoNascondi Nessuno
Azzi Abdelmalek il 18 Lug 2014
Modificato: Azzi Abdelmalek il 18 Lug 2014
What is the expected result for your example?

Accedi per commentare.

### Risposta accettata

Azzi Abdelmalek il 18 Lug 2014
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 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
Sameer il 18 Lug 2014
Thanks a lot

Accedi per commentare.

### Più risposte (1)

the cyclist il 18 Lug 2014
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.
rows = [1; 2];
and
cols = [2; 4];
cols seems to be the main thing you are going for.
##### 2 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Sameer il 18 Lug 2014
Thanks a lot

Accedi per commentare.

### Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by