Thread Subject: help with matrix vector multiplication

Subject: help with matrix vector multiplication

From: bolke

Date: 9 Nov, 2009 22:07:03

Message: 1 of 9

hello,

What would be the easiest way to let the first entry of a vector multiply the entire row of a matrix. An example would be:

 4 5 2 2 20 8 8
[3] * [3 0 2] = [9 0 6 ]
 5 7 4 4 35 20 20

I must be easier then taking each row separately like

x(1)*A(1,:)
x(2)*A(2,:)

which would accomplish what i want, but it seems to be so messy.

Subject: help with matrix vector multiplication

From: James Tursa

Date: 9 Nov, 2009 22:29:02

Message: 2 of 9

bolke <bhogendijk@gmail.com> wrote in message <1611896827.41489.1257804453524.JavaMail.root@gallium.mathforum.org>...
> hello,
>
> What would be the easiest way to let the first entry of a vector multiply the entire row of a matrix. An example would be:
>
> 4 5 2 2 20 8 8
> [3] * [3 0 2] = [9 0 6 ]
> 5 7 4 4 35 20 20
>
> I must be easier then taking each row separately like
>
> x(1)*A(1,:)
> x(2)*A(2,:)
>
> which would accomplish what i want, but it seems to be so messy.

bsxfun(@times,x(:),A)

James Tursa

Subject: help with matrix vector multiplication

From: dpb

Date: 9 Nov, 2009 22:45:16

Message: 3 of 9

bolke wrote:
> hello,
>
> What would be the easiest way to let the first entry of a vector
multiply the entire row of a matrix. An example would be:
>
> 4 5 2 2 20 8 8
> [3] * [3 0 2] = [9 0 6 ]
> 5 7 4 4 35 20 20
>
> I must be easier then taking each row separately like
>
> x(1)*A(1,:)
> x(2)*A(2,:)
>
> which would accomplish what i want, but it seems to be so messy.

Seems a somewhat unusual operation; think best would be to write a
function for it that does it via for loop for the input vector/array
based on the sizes

--

Subject: help with matrix vector multiplication

From: arun

Date: 9 Nov, 2009 23:19:51

Message: 4 of 9

On Nov 9, 11:45 pm, dpb <n...@non.net> wrote:
> bolke wrote:
> > hello,
>
> > What would be the easiest way to let the first entry of a vector
>
> multiply the entire row of a matrix. An example would be:
>
>
>
> >  4       5  2  2     20 8  8
> > [3]  *  [3  0  2] = [9  0  6 ]
> >  5       7  4  4     35 20 20
>
> > I must be easier then taking each row separately like
>
> > x(1)*A(1,:)
> > x(2)*A(2,:)
>
> > which would accomplish what i want, but it seems to be so messy.
>
> Seems a somewhat unusual operation; think best would be to write a
> function for it that does it via for loop for the input vector/array
> based on the sizes
>
> --

I don't understand what's unusual about multiplying each element of a
vector with each row of a matrix.
It could be done without a function and a loop.

@bolke
Alternative to the very handy solution Tursa provided,
you could also do it by,

m = 100; n = 1000;
a = ceil(m*rand(m,1));
b = ceil(m*rand(m,n));
%Tursa's code
u = bsxfun(@times, a(:),b);
%Alternative solution (pretty straightforward)
v = repmat(a,1,m) .* b;
isequal(u,v) % answer is 1

best, arun.

Subject: help with matrix vector multiplication

From: dpb

Date: 10 Nov, 2009 00:14:09

Message: 5 of 9

arun wrote:
...
> I don't understand what's unusual about multiplying each element of a
> vector with each row of a matrix.

Unusual enough there's not an operator defined for it.

> It could be done without a function and a loop.

Well, yeah, it could...

...

> Alternative to the very handy solution Tursa provided,

I have a version that predates bsxfun so that's not an option and so I
don't normally recommend it 'cuz have no assurance I'll get anything I
would say about it right...

...

> %Alternative solution (pretty straightforward)
> v = repmat(a,1,m) .* b;
...

Yes, at cost of the additional memory which as long as not too large
probably isn't much or any worse than the loop...

--

Subject: help with matrix vector multiplication

From: arun

Date: 10 Nov, 2009 00:51:11

Message: 6 of 9

@bolke
The line:
v = repmat(a,1,m) .* b;

should be:

v = repmat(a,1,n) .* b;

@dpb,

> Unusual enough there's not an operator defined for it.

First, assuming I have 5 vectors and 5 scalars and each of these
scalars have to be multiplied to the corresponding vector, I would do
it this way and not multiplying by taking each vector. Even then, the
operator would be the .* operator (at least in matlab).

> I have a version that predates bsxfun so that's not an option and so I
> don't normally recommend it 'cuz have no assurance I'll get anything I
> would say about it right...

Normally, the way I see is that people know how to program using loops
(unless they are beginners to programming itself). There are a
multitude of questions where people request for vectorized code by
providing their for-loops and not the other way around. At times, I
have seen some experienced programmers and contributors here
recommending the for-loop and NOT the vectorized approach stating
really intuitive and wonderful reasons.

> > %Alternative solution (pretty straightforward)
> > v = repmat(a,1,m) .* b;
> Yes, at cost of the additional memory which as long as not too large
> probably isn't much or any worse than the loop...

1) Matlab's inbuilt command bsxfun refers to repmat and arrayfun
internally and so, its no different in terms of memory usage to the
one-liner from Tursa (probably in terms of speed may be).

2) You chose Matlab. You have already compromised because you know
what you want. I think, there is no point in telling I could save
space by running a for-loop.

best, arun.

Subject: help with matrix vector multiplication

From: dpb

Date: 10 Nov, 2009 01:24:39

Message: 7 of 9

arun wrote:
> @bolke
> The line:
> v = repmat(a,1,m) .* b;
>
> should be:
>
> v = repmat(a,1,n) .* b;
>
> @dpb,
>
>> Unusual enough there's not an operator defined for it.
>
> First, assuming I have 5 vectors and 5 scalars and each of these
> scalars have to be multiplied to the corresponding vector, I would do
> it this way and not multiplying by taking each vector. Even then, the
> operator would be the .* operator (at least in matlab).
>
>> I have a version that predates bsxfun so that's not an option and so I
>> don't normally recommend it 'cuz have no assurance I'll get anything I
>> would say about it right...
>
> Normally, the way I see is that people know how to program using loops
> (unless they are beginners to programming itself). There are a
> multitude of questions where people request for vectorized code by
> providing their for-loops and not the other way around. At times, I
> have seen some experienced programmers and contributors here
> recommending the for-loop and NOT the vectorized approach stating
> really intuitive and wonderful reasons.
>
>>> %Alternative solution (pretty straightforward)
>>> v = repmat(a,1,m) .* b;
>> Yes, at cost of the additional memory which as long as not too large
>> probably isn't much or any worse than the loop...
>
> 1) Matlab's inbuilt command bsxfun refers to repmat and arrayfun
> internally and so, its no different in terms of memory usage to the
> one-liner from Tursa (probably in terms of speed may be).
>
> 2) You chose Matlab. You have already compromised because you know
> what you want. I think, there is no point in telling I could save
> space by running a for-loop.
>
> best, arun.

whatever...

I've no real clue what set you off, but ok.

--

Subject: help with matrix vector multiplication

From: Matt Fig

Date: 10 Nov, 2009 02:16:02

Message: 8 of 9

arun <aragorn168b@gmail.com> wrote in message <fd3df730-2fc4-4ec3-
> 1) Matlab's inbuilt command bsxfun refers to repmat and arrayfun
> internally and so, its no different in terms of memory usage to the
> one-liner from Tursa (probably in terms of speed may be).


bsxfun does not call repmat, or even a built-in version of repmat.
Try this to convince your self. Put this in a function M-File.

function c = myop(a,b)
c = a.*b % Leave of the semicolon for now.

Now from the command line:
>> a = magic(3); b = [1 3 2];
>> bsxfun(@myop,a,b)
c =
     8
     3
     4
c =
     3
    15
    27
c =
    12
    14
     4
ans =
     8 3 12
     3 15 14
     4 27 4


bsxfun IS more memory efficient than calling repmat.

Subject: help with matrix vector multiplication

From: arun

Date: 10 Nov, 2009 03:06:09

Message: 9 of 9

On Nov 10, 3:16 am, "Matt Fig" <spama...@yahoo.com> wrote:
> arun <aragorn1...@gmail.com> wrote in message <fd3df730-2fc4-4ec3-
> > 1) Matlab's inbuilt command bsxfun refers to repmat and arrayfun
> > internally and so, its no different in terms of memory usage to the
> > one-liner from Tursa (probably in terms of speed may be).
>
> bsxfun does not call repmat, or even a built-in version of repmat.
> Try this to convince your self.  Put this in a function M-File.
>
> function c = myop(a,b)
> c = a.*b  % Leave of the semicolon for now.
>
> Now from the command line:>> a = magic(3); b = [1 3 2];
> >> bsxfun(@myop,a,b)
>
> c =
>      8
>      3
>      4
> c =
>      3
>     15
>     27
> c =
>     12
>     14
>      4
> ans =
>      8     3    12
>      3    15    14
>      4    27     4
>
> bsxfun IS more memory efficient than calling repmat.  

Hi Matt,
Thank you, I am sorry about getting that wrong.
best, arun.

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