Matrix A & B need to be merged, Can I delete a complete raw if one appears in both matrices and same column position?
1 回表示 (過去 30 日間)
古いコメントを表示
I want to generate a matrix C out of A and B in condition that each 'one' in A in any column j should not appear in B in the same position. if this happen this row is discarded,
Ex1: A=[1 0 1]; B=[0 1 0; 0 0 0]; C=[1 1 1; 1 0 1] ((Two matrices are merged))
Ex2: A=[1 0 1]; B=[0 1 0; 1 0 0]; C=[1 1 1] ((Two matrices are merged
for first raw, second raw is deleted becuse 1 appears in column1 of A and B which violate the condition))
any help in this ?
0 件のコメント
採用された回答
James Tursa
2016 年 9 月 13 日
編集済み: James Tursa
2016 年 9 月 13 日
This was already answered in your other post, but I will repeat it here.
k = ~any(bsxfun(@and,A,B),2); % identify rows where there is no 1's overlap
C = bsxfun(@xor,A,B(k,:)); % xor A with the 'k' rows of B
This assumes A has only one row. If both A and B can have multiple rows, you need to tell us what the result should be. I used "xor" to be consistent with the other post, but I could have used "or" instead since we have already eliminated the rows with 1's overlap.
If A and B can have multiple rows, then you could use a loop to generate the individual row results and then concatenate them. E.g.,
m = size(A,1);
C = cell(m,1);
for n=1:m
k = ~any(bsxfun(@and,A(n,:),B),2); % identify rows where there is no 1's overlap
C{n} = bsxfun(@xor,A(n,:),B(k,:)); % xor A row with the 'k' rows of B
end
C = vertcat(C{:});
0 件のコメント
その他の回答 (1 件)
Andrei Bobrov
2016 年 9 月 14 日
Hi friends!
Please my small contribution:
jj = reshape(permute(...
bsxfun(@plus,permute(A,[3,2,1]),B),...
[1,3,2]),[],size(A,2));
C = jj(all(jj <= 1,2),:);
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!