フィルターのクリア

Remove consecutive duplicates from matrix

22 ビュー (過去 30 日間)
Simon Meyer
Simon Meyer 2020 年 11 月 26 日
コメント済み: Ameer Hamza 2020 年 11 月 30 日
Hello,
i need to remove duplicates form my matrix, but only if they are consecutive duplicates.
Lets suppose i have the following list of coordinates in a matrix, with for example x, y and z coordinates, that is describing a path.
A=[ 1, 0, 3
2, 1, 3
2, 1, 3
2, 2, 3
1, 0, 3]
Now, you can see that the point [2, 1, 3] is a consecutive duplicate, that I want to remove (because it's unuseful information).
But I do not want to remove the (duplicate) row at the end, [1, 0, 3], even if this row is already there in the beginning.
Also, the sorting shouldn't be changed. The desired output should be
A=[ 1, 0, 3
2, 1, 3
2, 2, 3
1, 0, 3]
I cannot use 'unique' for that, because that would filter also the last line.
What is the smartest way to do that?
Thank you very much for your help!

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 11 月 26 日
Try this
A=[ 1, 0, 3
2, 1, 3
2, 1, 3
2, 2, 3
1, 0, 3];
idx = find(~any(diff(A), 2))+1;
A(idx, :) = [];
  4 件のコメント
Simon Meyer
Simon Meyer 2020 年 11 月 30 日
Thank you very much! :)
Ameer Hamza
Ameer Hamza 2020 年 11 月 30 日
I am glad to be of help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeElementary Math についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by