From: us on
"Steve Amphlett" <Firstname.Lastname(a)Where-I-Work.com> wrote in message <i09nrl$t22$1(a)fred.mathworks.com>...
> nick <nghiango79(a)gmail.com> wrote in message <1489283667.14448.1277695292924.JavaMail.root(a)gallium.mathforum.org>...
> > This is what I am looking for. Thanks very much Roger.
>
> Before bsxfun there was repmat.
> Before repmat there was ones.

Before ones there was eval.
And eval was evil.

us
From: Andy on
nick <nghiango79(a)gmail.com> wrote in message <1489283667.14448.1277695292924.JavaMail.root(a)gallium.mathforum.org>...
> This is what I am looking for. Thanks very much Roger.

What was wrong with your original code? In fact, it was significantly faster:

X = rand(1000,1000);
Y = rand(1,1000);

tic;
for i=1:100
Z = [X(:,1)*Y(1) X(:,2)*Y(2) X(:,3)*Y(3)];
clear Z;
end
z1 = toc

tic;
for i =1:100
Z=bsxfun(@times, X,Y);
clear Z;
end
z2 = toc

% result:
% z1 = 0.0042
% z2 = 1.0881
From: John D'Errico on
"Andy " <theorigamist(a)gmail.com> wrote in message <i0a7d2$7cv$1(a)fred.mathworks.com>...
> nick <nghiango79(a)gmail.com> wrote in message <1489283667.14448.1277695292924.JavaMail.root(a)gallium.mathforum.org>...
> > This is what I am looking for. Thanks very much Roger.
>
> What was wrong with your original code? In fact, it was significantly faster:
>
> X = rand(1000,1000);
> Y = rand(1,1000);
>
> tic;
> for i=1:100
> Z = [X(:,1)*Y(1) X(:,2)*Y(2) X(:,3)*Y(3)];
> clear Z;
> end
> z1 = toc
>
> tic;
> for i =1:100
> Z=bsxfun(@times, X,Y);
> clear Z;
> end
> z2 = toc
>
> % result:
> % z1 = 0.0042
> % z2 = 1.0881

DUH. You are kidding of course?????????

Maybe, JUST maybe, if you compared apples to apples, you
might have gotten a sane response.

You do understand that your first test multiplies only THREE
columns of X and Y. The call to bsxfun multiplies EVERY
column of X with the corresponding element in Y.

There is a difference.

And of course, you might try writing out EVERY column.

Z = [X(:,1)*Y(1) X(:,2)*Y(2) X(:,3)*Y(3) X(:,4)*Y(4) ...
X(:,5)*Y(5) X(:,6)*Y(6) X(:,7)*Y(7) X(:,8)*Y(8) ...
X(:,9)*Y(9) X(:,10)*Y(10) X(:,11)*Y(11) X(:,12)*Y(12) ...
....];

Ok, I got bored with with it after 12 columns. Maybe you
have a higher tolerance for boredom.

John