remove rows in matrix if it is repeated 2 times

1 view (last 30 days)
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)

Accepted Answer

Guillaume
Guillaume on 17 Mar 2020
Edited: Guillaume on 17 Mar 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), :) = [];

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by