From: stani S. on 19 Jul 2010 17:10 Hi, I have 3 dim matrix A(x,y,z) and a vector V(1,z). I want to multiply each slice of A(:,:,b) with the scalar V(b), b=1:z. What is the fastest way to do it? Thanks!!!
From: Jan Simon on 19 Jul 2010 17:31 Dear stani, > I have 3 dim matrix A(x,y,z) and a vector V(1,z). I want to multiply each slice of A(:,:,b) with the scalar V(b), b=1:z. What is the fastest way to do it? bsxfun(@times, A, V) Jan
From: Jan Simon on 19 Jul 2010 17:41 Dear stani, > > > I have 3 dim matrix A(x,y,z) and a vector V(1,z). I want to multiply each slice of A(:,:,b) with the scalar V(b), b=1:z. What is the fastest way to do it? > > bsxfun(@times, A, V); *Wrong*. Then the 2nd dimension is multiplied. Better: bsxfun(@times, A, reshape(V, 1, 1, numel(V))) Now V is a [1 x 1 x z] array and the auto-expansion works as wanted. Jan
From: stani S. on 20 Jul 2010 12:49 Great! Thanks Jan! "Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <i22gpg$qr1$1(a)fred.mathworks.com>... > Dear stani, > > > > > I have 3 dim matrix A(x,y,z) and a vector V(1,z). I want to multiply each slice of A(:,:,b) with the scalar V(b), b=1:z. What is the fastest way to do it? > > > > bsxfun(@times, A, V); > *Wrong*. Then the 2nd dimension is multiplied. Better: > bsxfun(@times, A, reshape(V, 1, 1, numel(V))) > Now V is a [1 x 1 x z] array and the auto-expansion works as wanted. > > Jan
From: James Tursa on 10 Aug 2010 22:43 "Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <i22gpg$qr1$1(a)fred.mathworks.com>... > Dear stani, > > > > > I have 3 dim matrix A(x,y,z) and a vector V(1,z). I want to multiply each slice of A(:,:,b) with the scalar V(b), b=1:z. What is the fastest way to do it? > > > bsxfun(@times, A, reshape(V, 1, 1, numel(V))) > Now V is a [1 x 1 x z] array and the auto-expansion works as wanted. > > Jan Another option: mtimesx(A, reshape(V, 1, 1, numel(V))) MTIMESX can be found here: http://www.mathworks.com/matlabcentral/fileexchange/25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support James Tursa
|
Pages: 1 Prev: parfor reference to a cleared variable Next: Wrap around technique, Hexagon mapping to torus |