P. 1
MATLAB Array Manipulation Tips and Tricks

MATLAB Array Manipulation Tips and Tricks


|Views: 10,836|Likes:
Published by eebeta
from Internet
from Internet

More info:

Published by: eebeta on Feb 14, 2009
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF or read online from Scribd
See more
See less


Assume X is an m-by-n-by-p-by-q-by-... array and Y is a k-by-m matrix and you want to construct
a new k-by-n-by-p-by-q-by-... array Z, where

Z(:,:,i,j,...) = Y * X(:,:,i,j,...);

for alli=1,...,p, j=1,...,q, etc. This can be done with nested for-loops, or by the following
vectorized code

sx = size(X);
sy = size(Y);
Z = reshape(Y * X(:,:), [sy(1) sx(2:end)]);

The above works by reshaping X so that all 2D slices X(:,:,i,j,...) are placed next to each
other (horizontal concatenation), then multiply with Y, and then reshaping back again.
The X(:,:) is simply a short-hand for reshape(X, [sx(1) prod(sx)/sx(1)]).

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->