remove rows in matrix if it is repeated 2 times
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
A = {[1,6,3,2],[3,6,5]};
A1 = unique([A{:}]);
B = [1 2; 1 6; 2 3; 3 5; 3 6; 5 6];
B_bk = [1 2; 3 6; 3 5; 1 4; 4 6; 1 6; 6 5; 2 3; 6 7; 3 4];
remved_B = [];
for j=1:length(A1)
if (sum(sum(B == A1(j)),2)==2) && (sum(sum(B_bk == A1(j)),2)==2)
B_bk(any(ismember(B_bk,A1(j)),2),:)=[];
remved_B = [A1(j) remved_B];
end
end
I want to wirte this condition in a better way or remove for loop if possible
(sum(sum(B == A1(j)),2)==2) && (sum(sum(B_bk == A1(j)),2)==2)
1 Kommentar
Akzeptierte Antwort
Guillaume
am 17 Mär. 2020
Bearbeitet: Guillaume
am 17 Mär. 2020
The whole thing is unnecessarily complicated.
A = {[1,6,3,2],[3,6,5]};
A1 = unique([A{:}]);
B = [1 2; 1 6; 2 3; 3 5; 3 6; 5 6];
B_bk = [1 2; 3 6; 3 5; 1 4; 4 6; 1 6; 6 5; 2 3; 6 7; 3 4];
countinB = histcounts(B, [A1, Inf]); %the Inf to ensure that the last element of A1 is its own bin. See doc of histcounts for how edges are used
countinB_bk = histcounts(B_bk, [A1, Inf]);
removed_B = A1(countinB == 2 & countinB_bk == 2); %could also be written removed_B = A(all([countinB; countinB_bk] == 2));
B_bk(any(ismember(B_bk, removed_B), 2), :) = [];
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!