Delete rows with same elements
4 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
Star Strider
2018 年 11 月 25 日
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 件のコメント
その他の回答 (1 件)
Walter Roberson
2018 年 11 月 25 日
mask = all(diff(a, [], 2) == 0)
Now you can use mask as the row selector in deletion.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!