How to identify the repeated elements in an array and delete those rows?

3 次查看(过去 30 天)
I have a matrix array like this:
1 100
2 200
3 200
4 200
5 500
And I want to identify the repeated elements on the right column, and delete the entire row of the matrix, to get something like this:
1 100
2 200
5 500
How can I do that?

回答(2 个)

the cyclist
the cyclist 2023-4-9
编辑:the cyclist 2023-4-9
Here is one way:
M = [1 100;
2 200;
3 200;
4 200;
5 500];
[~,j] = unique(M(:,2));
out = M(j,:)
out = 3×2
1 100 2 200 5 500

Star Strider
Star Strider 2023-4-9
Use unique
A = [1 100
2 200
3 200
4 200
5 500];
[Au,ix] = unique(A(:,2),'stable');
Anew = A(ix,:)
Anew = 3×2
1 100 2 200 5 500
.

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by