필터 지우기
필터 지우기

Delete rows with same elements

조회 수: 8 (최근 30일)
klb
klb 2018년 11월 25일
댓글: Walter Roberson 2018년 11월 26일
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.

채택된 답변

Star Strider
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
klb
klb 2018년 11월 26일
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 2018년 11월 26일
As always, my pleasure.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 11월 25일
mask = all(diff(a, [], 2) == 0)
Now you can use mask as the row selector in deletion.

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by