Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Array multiplication by a vector
Replies: 5   Last Post: Jan 17, 2012 5:31 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Pag Max

Posts: 13
Registered: 5/16/11
Re: Array multiplication by a vector
Posted: Jan 17, 2012 5:06 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Thanks!!
Both are pretty cool solutions. I did think repmat would be a way to go about it but was not sure.
bsxfun is completely new to me. Thank you so much for showing that!!

Greg Heath <gheath01@gmail.com> wrote in message <8e26555b-98f2-4724-9acb-173bf94619b4@dp8g2000vbb.googlegroups.com>...
> On Jan 17, 4:09 am, "Pag Max" <tismarkhanNOS...@gamil.com> wrote:
> > I have a  M X N array and 1 X N vector. I am looking for another array of size M X N such that each of the N column of the original array are multiplied or scaled by corresponding element in the vector.
> >
> > This can be done very easily witha  loop but I want to know if there is a smarter way.
> >
> > example
> >

> > >> a=[1 2 3; 4 5 6];
> > >> b=[5 6 8];
> > >> for j=1:numel(b)

> >
> > c(:,j)=a(:,j)*b(j);
> > end
> > c =
> >
> >      5    12    24
> >     20    30    48

>
> >> a.*repmat(b,size(a,1),1)
>
> ans =
>
> 5 12 24
> 20 30 48
>
> Hope this helps
>
> Greg




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.