From: ErasmusMC on
Hi,
I would like to multiply a 1 dimensional vector "C" of certain length,say 12 with a 5 dimensional array "E" without using for-loops. The last dimension of this 5 dimensional array has the same length as vector C.

So I want to do this:

for i=1:12
A(:,:,:,:,i)=C(i).*E(:,:,:,:,i)
end

but without the for-loop.

Is there a method to do this in matlab?
Thanks in advance.
From: Matt J on
"ErasmusMC " <z.rijnen(a)erasmusmc.nl> wrote in message <hp4o8f$9on$1(a)fred.mathworks.com>...
> Hi,
> I would like to multiply a 1 dimensional vector "C" of certain length,say 12 with a 5 dimensional array "E" without using for-loops. The last dimension of this 5 dimensional array has the same length as vector C.
>
> So I want to do this:
>
> for i=1:12
> A(:,:,:,:,i)=C(i).*E(:,:,:,:,i)
> end
>
> but without the for-loop.

s=size(E);
A=reshape( reshape(E,[],12)*C(:) ,s);
From: Matt J on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hp4out$k20$1(a)fred.mathworks.com>...

> s=size(E);
> A=reshape( reshape(E,[],12)*C(:) ,s);

make that

A=reshape( reshape(E,[],12)*C(:) ,s(1:end-1));
From: ErasmusMC on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hp4pqh$38f$1(a)fred.mathworks.com>...
> "Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hp4out$k20$1(a)fred.mathworks.com>...
>
> > s=size(E);
> > A=reshape( reshape(E,[],12)*C(:) ,s);
>
> make that
>
> A=reshape( reshape(E,[],12)*C(:) ,s(1:end-1));

Hi Matt, thanks for the quick reply!
We tested this, however A=reshape( reshape(E,[],12)*C(:) ,s(1:end-1));
results in A being a 4 dimensional array while while we want a 5 dimensional array.
The multiplication must be done elementwise.

A litlle background:
The array E consist of 4 dimensions of data for 12 different fields.All 12 fields must be multiplicated seperatly. So our solution must result in A having the same size as E.
C and E are complex by the way...

thanks in advance
From: Matt J on
"ErasmusMC " <z.rijnen(a)erasmusmc.nl> wrote in message <hp4v01$rne$1(a)fred.mathworks.com>...

> Hi Matt, thanks for the quick reply!
> We tested this, however A=reshape( reshape(E,[],12)*C(:) ,s(1:end-1));
> results in A being a 4 dimensional array while while we want a 5 dimensional array.
> The multiplication must be done elementwise.
>
> A litlle background:
> The array E consist of 4 dimensions of data for 12 different fields.All 12 fields must be multiplicated seperatly. So our solution must result in A having the same size as E.
> C and E are complex by the way...
=========

OK. Well, the for-loop you already have is going to be the optimal way in terms of speed, assuming these dimensions that you've mentioned are the real ones, not just an example. However, just for fun, here's how you could do it without loops:

Cp=permute(C(:),[2 3 4 5 1] );
A=bsxfun(@times,E,Cp);