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:
Vectorizing

Subject: Vectorizing

From: Widt

Date: 29 Mar, 2011 10:59:05

Message: 1 of 7

Hi.

I'm a Matlab beginner and I hope someone can help me with this problem.

If I have a matrix A,
A=[1 0.5 1;0 1 0;1 1 1];
and then a scalar t,
t=2;
I can write;
R=t*T and get;
R=[2 1 2;0 2 0;2 2 2];
Now if t needs to be a vector of e.g.100.000 items and I want to multiply the matrix T with each of the items in t then;
R=t*T and R-t.*T gives "Error using mtimes or times. Matrix dimensions must agree.
My question is is it possible to vectorize this problem so that I don't need to calculate this using a loop.

Thanks.

Subject: Vectorizing

From: Sean de

Date: 29 Mar, 2011 12:42:02

Message: 2 of 7

"Widt" wrote in message <imse1o$gc0$1@fred.mathworks.com>...
> Hi.
>
> I'm a Matlab beginner and I hope someone can help me with this problem.
>
> If I have a matrix A,
> A=[1 0.5 1;0 1 0;1 1 1];
> and then a scalar t,
> t=2;
> I can write;
> R=t*T and get;
> R=[2 1 2;0 2 0;2 2 2];
> Now if t needs to be a vector of e.g.100.000 items and I want to multiply the matrix T with each of the items in t then;
> R=t*T and R-t.*T gives "Error using mtimes or times. Matrix dimensions must agree.
> My question is is it possible to vectorize this problem so that I don't need to calculate this using a loop.
>
> Thanks.

doc bsxfun

e.g.
T = [0 .5 1; 0 1 0; 1 1 1];
t = [1 2 3];
R = eye(3);
R - bsxfun(@times,t,T)

Subject: Vectorizing

From: ImageAnalyst

Date: 29 Mar, 2011 13:15:45

Message: 3 of 7

There is a difference between the multiplication of two matrices using
matrix multiplication and the multiplication using element-by-element
multiplication.
Matrix multiplication (http://en.wikipedia.org/wiki/
Matrix_multiplication) uses star, while element-by-element uses dot
star.

If you've taken any basic college course on linear algebra, you should
know what matrix multiplication is. The MATLAB help probably also
describes it somewhere.

Subject: Vectorizing

From: TideMan

Date: 29 Mar, 2011 19:15:34

Message: 4 of 7

On Mar 29, 11:59 pm, "Widt " <g...@st-andrews.ac.uk> wrote:
> Hi.
>
> I'm a Matlab beginner and I hope someone can help me with this problem.
>
> If I have a matrix A,
> A=[1 0.5 1;0 1 0;1 1 1];
> and then a scalar t,
> t=2;
> I can write;
> R=t*T and get;
> R=[2 1 2;0 2 0;2 2 2];
> Now if t needs to be a vector of e.g.100.000 items and I want to multiply the matrix T with each of the items in t then;
> R=t*T and R-t.*T gives "Error using mtimes or times. Matrix dimensions must agree.
> My question is is it possible to vectorize this problem so that I don't need to calculate this using a loop.
>
> Thanks.

You seem to be quite muddled...........
First, you define a matrix A, then you talk about a matrix T.
I guess A and T the same, but I don't know why you changed them.
A or T is a 3x3 matrix and you want to multiply it by a vector that is
either 100000x1 or 1x100000.
Well, as Matlab has told you, you can't do this.
The matrix and the vector are incompatible.
You need to think about what it is you are trying to achieve.
What is the expected size of the output matrix R?

Here's one way:
t=t(:); % Make sure t is a column vector
R=t*ones(1,3)*T; % Gives R(10000,3)

Subject: Vectorizing

From: Roger Stafford

Date: 29 Mar, 2011 19:39:05

Message: 5 of 7

"Widt" wrote in message <imse1o$gc0$1@fred.mathworks.com>...
> Hi.
>
> I'm a Matlab beginner and I hope someone can help me with this problem.
>
> If I have a matrix A,
> A=[1 0.5 1;0 1 0;1 1 1];
> and then a scalar t,
> t=2;
> I can write;
> R=t*T and get;
> R=[2 1 2;0 2 0;2 2 2];
> Now if t needs to be a vector of e.g.100.000 items and I want to multiply the matrix T with each of the items in t then;
> R=t*T and R-t.*T gives "Error using mtimes or times. Matrix dimensions must agree.
> My question is is it possible to vectorize this problem so that I don't need to calculate this using a loop.
>
> Thanks.
- - - - - - - - -
  Based on your description, "multiply the matrix T with each of the items in t", I would say you should use matlab's 'kron' function. That would provide you with 900,000 individual products (with your A example) in a very large matrix. Is that what you want?

Roger Stafford

Subject: Vectorizing

From: Widt

Date: 30 Mar, 2011 08:09:04

Message: 6 of 7

"Roger Stafford" wrote in message <imtcgp$p5t$1@fred.mathworks.com>...
> "Widt" wrote in message <imse1o$gc0$1@fred.mathworks.com>...
> > Hi.
> >
> > I'm a Matlab beginner and I hope someone can help me with this problem.
> >
> > If I have a matrix A,
> > A=[1 0.5 1;0 1 0;1 1 1];
> > and then a scalar t,
> > t=2;
> > I can write;
> > R=t*T and get;
> > R=[2 1 2;0 2 0;2 2 2];
> > Now if t needs to be a vector of e.g.100.000 items and I want to multiply the matrix T with each of the items in t then;
> > R=t*T and R-t.*T gives "Error using mtimes or times. Matrix dimensions must agree.
> > My question is is it possible to vectorize this problem so that I don't need to calculate this using a loop.
> >
> > Thanks.
> - - - - - - - - -
> Based on your description, "multiply the matrix T with each of the items in t", I would say you should use matlab's 'kron' function. That would provide you with 900,000 individual products (with your A example) in a very large matrix. Is that what you want?
>
> Roger Stafford

Yes, the kron function is what I have been looking for.
Thank you all.

Subject: Vectorizing

From: Matt J

Date: 30 Mar, 2011 14:32:04

Message: 7 of 7

"Widt" wrote in message <imuof0$hui$1@fred.mathworks.com>...
>
>
> Yes, the kron function is what I have been looking for.
=====================

If you'll be multiplying the output of kron by vectors, then you can reduce memory consumption by a factor of 9 and computation time by maybe a factor of 6 by using my KronProd class

http://www.mathworks.com/matlabcentral/fileexchange/25969-efficient-object-oriented-kronecker-product-manipulation

In the following comparison, I've used the data sizes you've mentioned you're working with,


%%Fake data
R=[2 1 2;0 2 0;2 2 2];
t=rand(100000,1);
x=rand(3,1);



tic; %%Using normal KRON

  K=kron(t,R);
 y1=K*x;

toc;%Elapsed time is 0.095768 seconds.


tic%%Using KronProd class

 Ko=KronProd({R,t});
 y2=Ko*x;
   y2=y2(:);

toc; %Elapsed time is 0.013979 seconds.


>>Error=norm(y1-y2)


 Error =

   2.4336e-014


>>whos K Ko %memory consumption


  Name Size Bytes Class Attributes

  K 300000x3 7200000 double
  Ko 300000x3 800356 KronProd

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