Got Questions? Get Answers.
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:
Vector multiplication with a row of matrix

Subject: Vector multiplication with a row of matrix

From: Pud Ji Janto

Date: 4 Jul, 2005 16:31:23

Message: 1 of 5

Hi all,

I want to know if it's possible to multiply this in vectorized way
Let V a vector of size 1xn
     M a matrix of size mxn

How can I multiply component-wise: V.* M(i,:) without loop?

Thanks in advance for any helps

Pud

Subject: Vector multiplication with a row of matrix

From: =?iso-8859-15?Q?D._Gr=F6gler?=

Date: 4 Jul, 2005 17:57:49

Message: 2 of 5

On Mon, 04 Jul 2005 16:31:23 +0200, Pud Ji Janto
<"r.m.p.pudjijanto_nosp4|\/|_"@hotmail.com> wrote:

> Hi all,
>
> I want to know if it's possible to multiply this in vectorized way
> Let V a vector of size 1xn
> M a matrix of size mxn
>
> How can I multiply component-wise: V.* M(i,:) without loop?
>
> Thanks in advance for any helps
>
> Pud


Hi Pud,

V1 = repmat(V,m,1); % Produces a mxn Matrix whos rows are all equal to V
M1 = V.*M; % Elementwise Multiplication


Dominik

Subject: Vector multiplication with a row of matrix

From: Wander

Date: 12 Jul, 2011 17:57:10

Message: 3 of 5

"=?iso-8859-15?Q?D._Gr=F6gler?=" <metabolismus35@mailforce.net> wrote in message <opstebynddj9iv2f@pc2-23l44-wxp.physiol.win2000.unizh.ch>...
> On Mon, 04 Jul 2005 16:31:23 +0200, Pud Ji Janto
> <"r.m.p.pudjijanto_nosp4|\/|_"@hotmail.com> wrote:
>
> > Hi all,
> >
> > I want to know if it's possible to multiply this in vectorized way
> > Let V a vector of size 1xn
> > M a matrix of size mxn
> >
> > How can I multiply component-wise: V.* M(i,:) without loop?
> >
> > Thanks in advance for any helps
> >
> > Pud
>
>
> Hi Pud,
>
> V1 = repmat(V,m,1); % Produces a mxn Matrix whos rows are all equal to V
> M1 = V.*M; % Elementwise Multiplication
>
>
> Dominik

Hi Pud and Dominik,

Yes, this works, but evaluating V1.*M (I assume you meant this instead of your V.*M) gets computationally intensive for large m. Is there a way in MatLab to do this that does not grow that much in m?

(Theoretically, there should be a way: for very large m, I would say it will eventually pay off to use a for loop in which you multiply a row of M by a single elements of V, instead of Dominiks method. However, MatLab wouldn't be MatLab if one could replace any forloop ;) )

Subject: Vector multiplication with a row of matrix

From: Florin Neacsu

Date: 12 Jul, 2011 18:09:10

Message: 4 of 5

Hello,

You could try bsxfun.

>bsxfun(@times,M,V)

I do not know if is faster than a loop or not (or even than repmat). You should test it. Also I believe there was a similar topic on "Answers" where Sean d and Matt Fig came up with solutions and timings.

Regards,
Florin

Subject: Vector multiplication with a row of matrix

From: Jim Rockford

Date: 12 Jul, 2011 18:41:55

Message: 5 of 5


This thread that I initiated with a similar question is probably
relevant:
http://groups.google.com/group/comp.soft-sys.matlab/browse_frm/thread/e05c074a771972f7/0e708f9743a9ab3d?hl=en&lnk=gst&q=jim+rockford


Jim




On Jul 12, 1:57 pm, "Wander " <wanderwad...@hotmail.com> wrote:
> "=?iso-8859-15?Q?D._Gr=F6gler?=" <metabolismu...@mailforce.net> wrote in message <opstebynddj9i...@pc2-23l44-wxp.physiol.win2000.unizh.ch>...
> > On Mon, 04 Jul 2005 16:31:23 +0200, Pud Ji Janto  
> > <"r.m.p.pudjijanto_nosp4|\/|_"@hotmail.com> wrote:
>
> > > Hi all,
>
> > > I want to know if it's possible to multiply this in vectorized way
> > > Let V a vector of  size 1xn
> > >      M a matrix of size  mxn
>
> > > How can I multiply component-wise:  V.* M(i,:) without loop?
>
> > > Thanks in advance for any helps
>
> > > Pud
>
> > Hi Pud,
>
> > V1 = repmat(V,m,1); % Produces a mxn Matrix whos rows are all equal to V
> > M1 = V.*M; % Elementwise Multiplication
>
> > Dominik
>
> Hi Pud and Dominik,
>
> Yes, this works, but evaluating V1.*M (I assume you meant this instead of your V.*M) gets computationally intensive for large m. Is there a way in MatLab to do this that does not grow that much in m?
>
> (Theoretically, there should be a way: for very large m, I would say it will eventually pay off to use a for loop in which you multiply a row of M by a single elements of V, instead of Dominiks method. However, MatLab wouldn't be MatLab if one could replace any forloop ;) )

Tags for 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