multiply each column matrix by an element

1099 views
Skip to first unread message

Karthik Ramanathan

unread,
May 3, 2004, 8:02:05 PM5/3/04
to
I have a matrix A (say of size n x m) and I want to multiply each column of
the matrix by w(1), w(2),... w(m), respectively.

say, A = [A1 A2 .... Am] where A1, A2, ... Am are column vectors.

I want the result B = [w(1)*A1 w(2)*A2 .... w(m)*Am]

I can get this by B = A*diag(w).

Is there a faster method than this or is this the most efficient method?
(for small m,n there won't be any difference but if m,n are large creating a
diagonal matrix and multiplying might take time)

Thanks
Karthik


Herbert Ramoser

unread,
May 4, 2004, 3:25:27 AM5/4/04
to

This is an alternative approach:

B = A .* repmat(w, size(A, 1), 1);

-Herbert

Ken Davis

unread,
May 4, 2004, 8:03:05 AM5/4/04
to
"Karthik Ramanathan" <rkar...@nospam.softhome.net> wrote in message
news:c76mi3$6d80$1...@masala.cc.uh.edu...

help spdiags. You can make a sparse diagonal matrix or you can use repmat.
When I have compared them, I think repmat was a little faster, but I still
prefer the sparse multiply for elegance. Both are considerably faster than
diag, which, as you have observed, does useless multiplies.


Reply all
Reply to author
Forward
0 new messages