- Use sort to sort the values in each row
- Use diff to get the difference between consecutive values in each row
- Use any to check if any of those differences equal zero (indicating a duplicate values)
- Use the output of the any command, which is a logical index, to delete the rows that have duplicates
how do I remove rows with duplicated values?
5 次查看(过去 30 天)
显示 更早的评论
let say i have matrix A
A = [1 2 3 4 5;
3 5 7 9 11;
1 1 4 5 7;
3 5 6 6 9;
1 4 10 15 16]
and wanted to remove all rows with values that repeated with each other. Then A will become
A = [1 2 3 4 5;
3 5 7 9 11;
1 4 10 15 16]
Thanks for the help in Advance
0 个评论
采纳的回答
the cyclist
2017-3-7
编辑:the cyclist
2017-3-7
Here is a one-liner:
A(any(diff(sort(A,2),[],2)==0,2),:)=[];
If you are certain that each row is already in numerical order (as in your example), you can skip the sorting step.
The algorithm is the following:
3 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!