Thread Subject: row by row multiplication by vector

Subject: row by row multiplication by vector

From: Chan Huntington

Date: 22 Nov, 2009 19:06:27

Message: 1 of 5

I expect to be embarrassed by the answer to this, but...

I need to multiply each row of a matrix by the same row in a vector. To do this with a loop is simple, but seemingly inelegant. Any better thoughts.

The loop code would like like:
x = magic(3);
y = [1,2,3];
for i = 1:3
t = x(i,:)*y(i)
end

Thanks!

Subject: row by row multiplication by vector

From: José David

Date: 22 Nov, 2009 19:54:25

Message: 2 of 5

On 22 nov, 20:06, Chan Huntington <chann...@umich.edu> wrote:
> I expect to be embarrassed by the answer to this, but...
>
> I need to multiply each row of a matrix by the same row in a vector.  To do this with a loop is simple, but seemingly inelegant.  Any better thoughts.
>
> The loop code would like like:
> x = magic(3);
> y = [1,2,3];
> for i = 1:3
> t = x(i,:)*y(i)
> end
>
> Thanks!

Hi,

Maybe you can use something like this

n=3;
x = magic(n);
y = [1,2,3];
k=kron(y,x.');
t=(k(:,[1,n+2,2*n+3])).';

The last line just selects the rows that you want.

Greetings.

Subject: row by row multiplication by vector

From: Doug Schwarz

Date: 22 Nov, 2009 19:57:29

Message: 3 of 5

In article
<737102407.20922.1258916818172.JavaMail.root@gallium.mathforum.org>,
 Chan Huntington <channing@umich.edu> wrote:

> I expect to be embarrassed by the answer to this, but...
>
> I need to multiply each row of a matrix by the same row in a vector. To do
> this with a loop is simple, but seemingly inelegant. Any better thoughts.
>
> The loop code would like like:
> x = magic(3);
> y = [1,2,3];
> for i = 1:3
> t = x(i,:)*y(i)
> end
>
> Thanks!

Check out bsxfun.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: row by row multiplication by vector

From: Jan Simon

Date: 22 Nov, 2009 20:07:01

Message: 4 of 5

Dear Chan!

> I need to multiply each row of a matrix by the same row in a vector. To do this with a loop is simple, but seemingly inelegant. Any better thoughts.
>
> x = magic(3);
> y = [1,2,3];
> for i = 1:3
> t = x(i,:)*y(i)
> end

At first I assume, that the rsulting t should be a matrix:
  x = magic(3);
  y = [1,2,3];
  for i = 1:3
    t(i, :) = x(i,:)*y(i)
  end

Method 1: ONES
  t = x .* y(ones(1, 3), :)'

Method 2: BSXFUN:
  t = bsxfun(@times, x, y')

Kind regards, Jan

Subject: row by row multiplication by vector

From: Chan Huntington

Date: 22 Nov, 2009 20:21:04

Message: 5 of 5

I always know matlab has clever ways to do these things, just need to find them.... bsxfun it is, thanks all!

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com