Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Multiply the ith column of a matrix by element at position i of a vector

Subject: Multiply the ith column of a matrix by element at position i of a vector

From: Misha Koshelev

Date: 10 May, 2009 15:08:02

Message: 1 of 6

Hi, I would like to do something like:

for i=1:cols(M)
     M(:,i) = M(:,i)*v(i)
end

Is there a fast/vectorized way to do this? Is this faster:

M = M*repmat(v,rows(M),1);

Thank you
Misha

Subject: Multiply the ith column of a matrix by element at position i of a

From: Greg Heath

Date: 10 May, 2009 15:26:35

Message: 2 of 6

On May 10, 11:08 am, "Misha Koshelev" <mk144...@bcm.edu> wrote:
> Hi, I would like to do something like:
>
> for i=1:cols(M)
>      M(:,i) = M(:,i)*v(i)
> end
>
> Is there a fast/vectorized way to do this? Is this faster:
>
> M = M*repmat(v,rows(M),1);

That does not work.

If

A = R*M* C

where R and C are diagonal, then

a(i,j) = r(i)*m(i,j)*c(j)

i.e., r(i) multiplies each element in the ith row and c(j)
multiplies each element in the jth column. So, if r and c
are vectors,

A = diag(r)*M*diag(c)

Hope this helps.

Greg

Subject: Multiply the ith column of a matrix by element at position i of a vector

From: Matt Fig

Date: 10 May, 2009 15:30:03

Message: 3 of 6

% Data
 M = magic(5)
v = [6 7 8 9 10]

% Engine
D1 = bsxfun(@times,M,v);


% Compare to other method
for i=1:size(M,2)
     D2(:,i) = M(:,i)*v(i);
end

all(D1(:)=Ò(:))

Subject: Multiply the ith column of a matrix by element at position i of a vector

From: Jos

Date: 11 May, 2009 06:02:01

Message: 4 of 6

"Misha Koshelev" <mk144210@bcm.edu> wrote in message <gu6qkh$3ut$1@fred.mathworks.com>...
> Hi, I would like to do something like:
>
> for i=1:cols(M)
> M(:,i) = M(:,i)*v(i)
> end
>
> Is there a fast/vectorized way to do this? Is this faster:
>
> M = M*repmat(v,rows(M),1);
>
> Thank you
> Misha

In general, use BSXFUN; if M is square (NxN) you can also use

M * diag(v)

hth
Jos

Subject: Multiply the ith column of a matrix by element at position i of a vector

From: Bruno Luong

Date: 11 May, 2009 06:21:17

Message: 5 of 6

"Jos " <#10584@fileexchange.com> wrote in message <gu8f0p$ci1$1@fred.mathworks.com>...

> if M is square (NxN) you can also use
>
> M * diag(v)
>

This works even for M is (M x N) rectangle, the only requirement is v has N elements.

Bruno

Subject: Multiply the ith column of a matrix by element at position i of a vector

From: Jos

Date: 11 May, 2009 06:41:01

Message: 6 of 6

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gu8g4t$pg0$1@fred.mathworks.com>...
> "Jos " <#10584@fileexchange.com> wrote in message <gu8f0p$ci1$1@fred.mathworks.com>...
>
> > if M is square (NxN) you can also use
> >
> > M * diag(v)
> >
>
> This works even for M is (M x N) rectangle, the only requirement is v has N elements.
>
> Bruno

Yep! Thanks for correcting me, Bruno.

Tags for this Thread

No tags are associated with this thread.

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.

Contact us