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:
Pointwise multiplication of matrix with vector

Subject: Pointwise multiplication of matrix with vector

From: Marius

Date: 27 Jan, 2006 08:58:15

Message: 1 of 8

Hello,

In Matlab one can write
c = 3; % (some scalar)
x = 0:10; % (some vector)
y = c * x; % (multiply every component of x with c)

This is much faster than using a for loop and writing
for k = 1:length(x)
  y(k) = c * x(k);
end

I would now like to do a similar thing, but where the scalar is
replaced by a n x 1 vector and the vector is replaced by a n x m
matrix. I.e., I would like to multiply each column of a matrix
pointwise with a column vector.

So far I can see two solutions for this (besides a double for loop):

1. Use repmat to create m copies of the colum vector and multiply the
resulting matrix pointwise with the original matrix:

% Given a vector v (n x 1) and matrix A (n x m):
V = repmat(v, 1, m);
B = A .* V;

2. Use a for loop to go through each element of the vector and thus
reduce to the problem to the <scalar> * <vector>
situation:

for k = 1:length(v)
  B(1, :) = A(1, :) * v(k);
end

I have found the second solution to be faster than the first one. Is
there a better solution?

Thanks
Marius

Subject: Pointwise multiplication of matrix with vector

From: Doug Schwarz

Date: 27 Jan, 2006 14:02:28

Message: 2 of 8

In article <ef26e13.-1@webx.raydaftYaTP>,
 Marius <noemail.here@hotmail.com> wrote:

> Hello,
>
> In Matlab one can write
> c = 3; % (some scalar)
> x = 0:10; % (some vector)
> y = c * x; % (multiply every component of x with c)
>
> This is much faster than using a for loop and writing
> for k = 1:length(x)
> y(k) = c * x(k);
> end
>
> I would now like to do a similar thing, but where the scalar is
> replaced by a n x 1 vector and the vector is replaced by a n x m
> matrix. I.e., I would like to multiply each column of a matrix
> pointwise with a column vector.
>
> So far I can see two solutions for this (besides a double for loop):
>
> 1. Use repmat to create m copies of the colum vector and multiply the
> resulting matrix pointwise with the original matrix:
>
> % Given a vector v (n x 1) and matrix A (n x m):
> V = repmat(v, 1, m);
> B = A .* V;
>
> 2. Use a for loop to go through each element of the vector and thus
> reduce to the problem to the <scalar> * <vector>
> situation:
>
> for k = 1:length(v)
> B(1, :) = A(1, :) * v(k);
> end
>
> I have found the second solution to be faster than the first one. Is
> there a better solution?
>
> Thanks
> Marius

You might find this to be interesting.

  <http://www.frontiernet.net/~dmschwarz/genops.html>

I don't know if it's faster, but it works without replicating data.

--
Doug Schwarz
dmschwarz&urgrad,rochester,edu
Make obvious changes to get real email address.

Subject: Pointwise multiplication of matrix with vector

From: French Caroline

Date: 27 Jan, 2006 09:15:37

Message: 3 of 8

3rd solution :
B = diag(v)*A
Caroline

Subject: Pointwise multiplication of matrix with vector

From: Michael Wild

Date: 27 Jan, 2006 15:10:33

Message: 4 of 8

Marius wrote:
> Hello,
>
> In Matlab one can write
> c = 3; % (some scalar)
> x = 0:10; % (some vector)
> y = c * x; % (multiply every component of x with c)
>
> This is much faster than using a for loop and writing
> for k = 1:length(x)
> y(k) = c * x(k);
> end
>
> I would now like to do a similar thing, but where the scalar is
> replaced by a n x 1 vector and the vector is replaced by a n x m
> matrix. I.e., I would like to multiply each column of a matrix
> pointwise with a column vector.
>
> So far I can see two solutions for this (besides a double for loop):
>
> 1. Use repmat to create m copies of the colum vector and multiply the
> resulting matrix pointwise with the original matrix:
>
> % Given a vector v (n x 1) and matrix A (n x m):
> V = repmat(v, 1, m);
> B = A .* V;
>
> 2. Use a for loop to go through each element of the vector and thus
> reduce to the problem to the <scalar> * <vector>
> situation:
>
> for k = 1:length(v)
> B(1, :) = A(1, :) * v(k);
> end
>
> I have found the second solution to be faster than the first one. Is
> there a better solution?
>
> Thanks
> Marius

http://www.frontiernet.net/~dmschwarz/genops.html provides generalized
operators.

michael

Subject: Pointwise multiplication of matrix with vector

From: kleiner@gmail.com

Date: 28 Jan, 2006 01:12:42

Message: 5 of 8

Thanks, exactly what I was looking for.
However, in order to compile it on Matlab 7.1 (R14) on Mac OS X, I had
to make the following changes:

- In makegenops.m, change the filename on l.31 from 'genops_noflops.c'
to 'genops.c'
- In genops.c, change the following lines, wherever they occur:
      zMat = mxCreateNumericArray(ndim, s, mxDOUBLE_CLASS, mxREAL);
      mxSetLogical(zMat);
to this:
      zMat = mxCreateLogicalArray(ndim, s);

This seems to make it work fine. (mxSetLogical has been obsolete since
Matlab 6.5).
(Doug please comment if you think that will break the functionality)

Best
Marius

Subject: Pointwise multiplication of matrix with vector

From: Doug Schwarz

Date: 30 Jan, 2006 05:44:27

Message: 6 of 8

In article <1138439562.215231.80630@o13g2000cwo.googlegroups.com>,
 kleiner@gmail.com wrote:

> Thanks, exactly what I was looking for.
> However, in order to compile it on Matlab 7.1 (R14) on Mac OS X, I had
> to make the following changes:
>
> - In makegenops.m, change the filename on l.31 from 'genops_noflops.c'
> to 'genops.c'
> - In genops.c, change the following lines, wherever they occur:
> zMat = mxCreateNumericArray(ndim, s, mxDOUBLE_CLASS, mxREAL);
> mxSetLogical(zMat);
> to this:
> zMat = mxCreateLogicalArray(ndim, s);
>
> This seems to make it work fine. (mxSetLogical has been obsolete since
> Matlab 6.5).
> (Doug please comment if you think that will break the functionality)
>
> Best
> Marius

Marius,

That looks right and thanks for pointing these things out.

I originally wrote this back when MATLAB counted flops so I had my
functions update the flop counter. I updated the code by removing the
flop counting stuff when MATLAB eliminated it and called that version
genops_noflops.c. Eventually, I deleted the flops version, but it seems
that I didn't change makegenops.

Obviously, the second change is also due to the age of this code.

I'm glad to hear that it works for you.

--
Doug Schwarz
dmschwarz&urgrad,rochester,edu
Make obvious changes to get real email address.

Subject: Pointwise multiplication of matrix with vector

From: Daniel

Date: 27 Jul, 2014 23:24:09

Message: 7 of 8

Excellent.
Somebody knows the same for matrix multiplication? mtimes?
Thanks!

Subject: Pointwise multiplication of matrix with vector

From: James Tursa

Date: 28 Jul, 2014 15:36:17

Message: 8 of 8

"Daniel" wrote in message <lr41mp$bks$1@newscl01ah.mathworks.com>...
> Excellent.
> Somebody knows the same for matrix multiplication? mtimes?
> Thanks!

If you mean nD matrix multiplication using 2D planes of the nD matrices, then there are submissions on the FEX that do this. E.g.

http://www.mathworks.com/matlabcentral/fileexchange/25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support

http://www.mathworks.com/matlabcentral/fileexchange/37515-mmx-multithreaded-matrix-operations-on-n-d-matrices

http://www.mathworks.com/matlabcentral/fileexchange/37515-mmx-multithreaded-matrix-operations-on-n-d-matrices

James Tursa

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