identify duplicate rows in a matrix

51 vues (au cours des 30 derniers jours)
Ananya Malik
Ananya Malik le 6 Oct 2016
Réponse apportée : KSSV le 6 Oct 2016
I have a matrix
A = [1 2 3; 3 4 5; 1 2 3];
I want to identify the duplicate row i.e. 3rd row and replace the values in that row by 0.
Resultant A = [1 2 3; 3 4 5; 0 0 0];
Is there an efficient way to do this? Thanks in Advance.

Réponse acceptée

Massimo Zanetti
Massimo Zanetti le 6 Oct 2016
You can identify the repeated rows by invoking unique function of Matlab, and then set to 0 non-unique rows as follows:
[C,ia,ib]=unique(A,'rows','stable');
i=true(size(A,1),1);
i(ia)=false;
A(i,:)=0;
Then A will be your output.

Plus de réponses (1)

KSSV
KSSV le 6 Oct 2016
A = [1 2 3; 3 4 5; 1 2 3];
[C,ia,ic] = unique(A,'rows') ;
iwant = zeros(size(A)) ;
iwant(ia,:) = C ;

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by