a=[2 3 2;3 3 3;4 4 4;2 5 4; 3 5 5; 4 4 4; 7 3 4]
how do I delete only those rows where elemets repeat for entire row length. In this example matrix, the three rows with all 3s and all 4s where this happens.

 Akzeptierte Antwort

Star Strider
Star Strider am 25 Nov. 2018

1 Stimme

Try this:
a=[2 3 2;3 3 3;4 4 4;2 5 4; 3 5 5; 4 4 4; 7 3 4];
a_new = a(all(diff(a,[],2) ~= 0, 2),:)
If all the columns in a particular row are the same, the vector returned by the diff function will all be uniformly 0. The all function across rows (dimension = 2) detects that, and deletes those rows.
a =
2 3 2
3 3 3
4 4 4
2 5 4
3 5 5
4 4 4
7 3 4
a_new =
2 3 2
2 5 4
7 3 4

3 Kommentare

klb
klb am 26 Nov. 2018
i want only the row with all elements same to be eliminated. so n ,n, x >> keep but n, n ,n>> delete
klb
klb am 26 Nov. 2018
ok i repalced 'all' with 'any' and that gives the requried answer. But thank you! Now on to researching the finess between all and any...
Star Strider
Star Strider am 26 Nov. 2018
As always, my pleasure.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 25 Nov. 2018

0 Stimmen

mask = all(diff(a, [], 2) == 0)
Now you can use mask as the row selector in deletion.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by