# How to separate the elements if any two elements of a row in a matrix is same in other rows of the matrix?

3 visualizzazioni (ultimi 30 giorni)
obaid-ur-rehman il 19 Gen 2018
Commentato: obaid-ur-rehman il 20 Gen 2018
A=[1 2 5;
2 3 5;
3 4 5;
1 5 6;
1 2 3]
Suppose I have the above A matrix. Now I will compare the elements of row one with all other rows of the matrix and if any two elements of other rows become same with the first row then i want to separate them. Similarly, I need to check for rest of the rows.
Like, here 2 5 of first row is also in 2nd row, 1 5 is in 4th row and 1 2 is in fifth row. SO i will store them in a matrix B=[2 5;1 5;1 2]. Similarly foe 2nd row, 3 5 is in third row,2 3 is in 5th row; so B matrix will be updated as B=[2 5;1 5;1 2;3 5;2 3]
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
KSSV il 19 Gen 2018

Accedi per commentare.

### Risposta accettata

Birdman il 19 Gen 2018
Modificato: Birdman il 19 Gen 2018
You may also use intersect and setdiff. For instance, here is an approach for the situation:
for i=1:size(A,1)
temp=setdiff(1:size(A,1),i);
for j=1:numel(temp)
B{i,j}=intersect(A(i,:),A(temp(j),:));
end
end
This compares the ith row of A matrix with the remaining ones and stores the different values in a cell array.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
obaid-ur-rehman il 20 Gen 2018
Thanks a lot for your kind help.

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