remove rows in matrix if it is repeated 2 times

조회 수: 1 (최근 30일)
NA
NA 2020년 3월 17일
편집: Guillaume 2020년 3월 17일
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)

채택된 답변

Guillaume
Guillaume 2020년 3월 17일
편집: Guillaume 2020년 3월 17일
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), :) = [];

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by