Filter löschen
Filter löschen

Column-wise AND operation in all column combinations of two matrices

10 Ansichten (letzte 30 Tage)
Given matrix a of size MxN and matrix b of size MxL I want to get a matrix C of size M x(N*L) which contains the column-wise logical operation AND of all combinations of columns of matrix a and matrix b.
a = [1 0 0
1 0 0
1 0 0
1 0 0
0 1 0
0 1 0
0 1 0
0 1 0
0 0 1
0 0 1
0 0 1
0 0 1];
b= [0 0 1
0 1 0
1 0 0
0 0 1
0 1 0
1 0 0
0 0 1
0 1 0
1 0 0
0 0 1
0 1 0
1 0 0];
C=[];
for ii=1:size(a, 2)
for jj=1:size(b, 2)
C = [C b(:,jj) & a(:,ii)];
end
end
I am searching for an alternative syntax of the above loops. Thank you!
  1 Kommentar
Jos (10584)
Jos (10584) am 24 Feb. 2014
A matrix of size MxN, means M rows and N columns, rather than the other way around ...

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Iain
Iain am 24 Feb. 2014
This should do it:
C = bsxfun(@and, a, reshape(b,[12,1,3]);
theresult = C(down,column_a,column_b);

Weitere Antworten (3)

Azzi Abdelmalek
Azzi Abdelmalek am 24 Feb. 2014
out=reshape(repmat(a,size(b,2),1),size(a,1),[]) & repmat(b,1,size(a,2))

Jos (10584)
Jos (10584) am 24 Feb. 2014
% Small example: two logical arrays
A = rand(3,3) > .25
B = rand(2,3) > .25
% general engine
[ib,ia] = ndgrid(1:size(B,1), 1:size(A,1))
C = A(ia(:),:) & B(ib(:),:)

Giorgos Papakonstantinou
Giorgos Papakonstantinou am 24 Feb. 2014
Thank you all for your replies!!

Kategorien

Mehr zu Descriptive Statistics finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by