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)
  1 件のコメント
KSSV
KSSV 2020 年 3 月 17 日
Check the rows option in ismember.

サインインしてコメントする。

採用された回答

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 Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by