Search All of the Math Forum:

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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

 Messages: [ Previous | Next ]
 Pag Max Posts: 13 Registered: 5/16/11
Re: Array multiplication by a vector
Posted: Jan 17, 2012 5:06 AM

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

Date Subject Author
1/17/12 Pag Max
1/17/12 Nasser Abbasi
1/17/12 gheath01@gmail.com
1/17/12 Pag Max
1/17/12 Bruno Luong
1/17/12 Pag Max