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:
Last question about matrix arrays...

Subject: Last question about matrix arrays...

From: Mauro

Date: 6 Oct, 2010 15:15:36

Message: 1 of 4

Suppose I have a matrix array X of dimension (n,n,M). I want to
multply each of the nxn matrices in X by a different value in a vector
V, of dimension M. How can I do that without a for loop?

Subject: Last question about matrix arrays...

From: Sean

Date: 6 Oct, 2010 15:25:26

Message: 2 of 4

Mauro <msmscarlatti@googlemail.com> wrote in message <1ee4c442-5790-4c5f-a886-021343249b85@i13g2000yqd.googlegroups.com>...
> Suppose I have a matrix array X of dimension (n,n,M). I want to
> multply each of the nxn matrices in X by a different value in a vector
> V, of dimension M. How can I do that without a for loop?

n = 4;m = 5;
A = repmat(magic(n),[1 1 m]);
B = rand(1,1,m);

bsxfun(@times,A,B)

%Once Again: play with bsxfun on your own!

Subject: Last question about matrix arrays...

From: James Tursa

Date: 6 Oct, 2010 16:14:05

Message: 3 of 4

Mauro <msmscarlatti@googlemail.com> wrote in message <1ee4c442-5790-4c5f-a886-021343249b85@i13g2000yqd.googlegroups.com>...
> Suppose I have a matrix array X of dimension (n,n,M). I want to
> multply each of the nxn matrices in X by a different value in a vector
> V, of dimension M. How can I do that without a for loop?

bsxfun(@times,X,reshape(V,1,1,M))

or

mtimesx(X,reshape(V,1,1,M))

mtimesx can be found on the FEX here:

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

James Tursa

Subject: Last question about matrix arrays...

From: Matt J

Date: 6 Oct, 2010 16:54:04

Message: 4 of 4

Another option is with the KronProd tool here

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

For example,

%fake data
n=3; M=3;
A=repmat(eye(n),[1,1, M]);
V=rand(1,M);


K=KronProd({1,sparse(diag(V))},[1 1 2],[n,n,M]);

theResult=K*A,

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