Multiply each row of a matrix by a different scalar

21 views
Skip to first unread message

Asaf Weinstein

unread,
May 13, 2015, 9:20:39 AM5/13/15
to israel-r-user-group
Hi,

I have a vector v (length m) and a matrix A (dim m-by-n). 
I would like to return the matrix whose i-th row is v[i] * A[i,]. What's the simplest way?

Thank you!
Asaf

amit gal

unread,
May 13, 2015, 9:41:51 AM5/13/15
to israel-r-...@googlegroups.com

Transpose the matrix. Use standard element-wise multiplication, and then transpose back. Should do the job

t(t(A)*v)

--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Asaf Weinstein

unread,
May 13, 2015, 9:54:17 AM5/13/15
to israel-r-user-group
Toda Amit!
Works well, only no need for transposing A, ie use simply: t(A*v)

amit gal

unread,
May 13, 2015, 9:57:28 AM5/13/15
to israel-r-...@googlegroups.com

Yes. My mistake. No need to transpose, unless you want to work on columns rather than rows. I mixed things up.

Reply all
Reply to author
Forward
0 new messages