The fastest I can give you is 4 seconds at the moment (on my machine). I'm not 100% sure if you've tried this or not, but here you go anyway.
I have an m-file randbool.m
with these contents, to generate the test data.
function x = randbool(m,n)
x = logical(rand(m,n) < 0.5);
Generate the data for testing:
>> u = randbool(1,20000);
>> M = randbool(30,20000);
I can think of three ways to loop over the rows of M (use bsxfun
, use repmat
, use a loop) and two ways to pull out the elements you want (conjunction &
, or pointwise multiplication with .*
). The fastest is the combination of bsxfun
and conjunction:
Bsxfun / conjunction
>> tic, for i=1:1000, bsxfun(@and,u,M); end, toc
Elapsed time is 4.068684 seconds.
Bsxfun / multiplication
>> tic, for i=1:1000, bsxfun(@times,u,M); end, toc
Elapsed time is 4.856784 seconds.
Repmat / conjunction
>> tic, for i=1:1000, utmp=repmat(u,30,1); M&utmp; end, toc
Elapsed time is 7.305158 seconds.
Repmat / multiplication
>> tic, for i=1:1000, utmp=repmat(u,30,1); M.*utmp; end, toc
Elapsed time is 8.117164 seconds.
Looping / conjunction
>> tic, for i=1:1000, for j = 1:30, out(j,:)=u&M(j,:); end; end, toc
Elapsed time is 7.110872 seconds.
Looping / multiplication
>> tic, for i=1:1000, for j = 1:30, out(j,:)=u.*M(j,:); end; end, toc
Elapsed time is 8.322888 seconds.