remove rows in matrix if it is repeated 2 times

1 visualización (últimos 30 días)
NA
NA el 17 de Mzo. de 2020
Editada: Guillaume el 17 de Mzo. de 2020
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)

Respuesta aceptada

Guillaume
Guillaume el 17 de Mzo. de 2020
Editada: Guillaume el 17 de Mzo. de 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), :) = [];

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by