How can I create a unique matrix by removing rows with similar elements?

1 Ansicht (letzte 30 Tage)
A = [11 12 1 3
8 9 10 12
227 228 217 219
217 218 8 7
224 225 226 228
443 444 433 435
433 434 224 223
440 441 442 444
659 660 649 651
649 650 440 439
656 657 658 660
875 876 865 867
865 866 656 655
872 873 874 876
1091 1092 1081 1083
1081 1082 872 871
1088 1089 1090 1092
1307 1308 1297 1299
1297 1298 1088 1087
1304 1305 1306 1308
1523 1524 1513 1515
1513 1514 1304 1303
1520 1521 1522 1524
3 1 12 11
12 10 9 8
6 4 3 2
9 7 6 5
219 217 228 227
228 226 225 224
222 220 219 218]
In A, I want to delete rows with similar elements. Because the elements in rows 1 and 3 are the same as those in rows 24 and 28, I want to delete 24 and 28. The output should look like this:
A_new = [[11 12 1 3
8 9 10 12
227 228 217 219
217 218 8 7
224 225 226 228
443 444 433 435
433 434 224 223
440 441 442 444
659 660 649 651
649 650 440 439
656 657 658 660
875 876 865 867
865 866 656 655
872 873 874 876
1091 1092 1081 1083
1081 1082 872 871
1088 1089 1090 1092
1307 1308 1297 1299
1297 1298 1088 1087
1304 1305 1306 1308
1523 1524 1513 1515
1513 1514 1304 1303
1520 1521 1522 1524
12 10 9 8
6 4 3 2
9 7 6 5
228 226 225 224
222 220 219 218]

Akzeptierte Antwort

Prakash S R
Prakash S R am 27 Apr. 2022
Hi,
You sort the rows so they are order-independent, then you can use unique() with the 'rows' option. However, the results are sorted along the row axis, which you will need to undo to get the result you want:
[B, Bi] = unique(sort(A,2), 'rows');
A_new = A(Bi,:);
-Prakash

Weitere Antworten (1)

ClementJ
ClementJ am 27 Apr. 2022
Hi,
Maybe, you can use:
%% load A data
load('A.mat')
%% process
sum_A = sum(A,2);
[~,ia,~] = unique(sum_A);
%% change A value
A_news = A(ia,:);
  4 Kommentare
Akira Agata
Akira Agata am 27 Apr. 2022
Bearbeitet: Akira Agata am 27 Apr. 2022
Seems strange... It should work.
Let me check by small example:
% Small example
A = [...
1 2 3;...
4 6 5;...
1 1 1;...
1 3 2]; % <- same elements with 1st row and should be removed
% Process
sort_A = sort(A, 2);
[~, ia] = unique(sort_A, 'stable', 'rows');
A_new = A(ia, :);
% Result
disp(A_new)
1 2 3 4 6 5 1 1 1

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by