From: Bernd on
Hello!

I need to speed up the execution of the following simple code.
Unfortunaty I don't have an idea how to do this with 3D Matixes. (normal
matrix multiplication doesn't work)

=====================
% size S = (n_spec,n_React) = (10,3)
% Size r = (n_cell,n_React,n_spec) = (20,3,10)
% a = skalar

S_p=zeros(n_cell,n_spec);
for ireact=1:n_React
for ispec=1:n_spec
S_p(:,ispec) =S_p(:,ispec)+ S(ispec,ireact) * r(:,ireact,ispec)
end
end;
=====================

If required, i could define 'S' as the transposed Matrix of 'S'


Thank you,
Bernd
From: Bruno Luong on
You could try this tool:

http://www.mathworks.com/matlabcentral/fileexchange/24260-multiple-same-size-linear-solver

S=rand(10,3);
r=rand(20,3,10)

S_p = Multiprod(r,S')

% Bruno
From: Bernd on
Bruno Luong schrieb:

> S_p = Multiprod(r,S')

Thank you for this hint, Bruno.

I forgot to mention, that I use this as 'Embedded Matlab Block' in a
Simulink modell. Unfortunatly sparse matrixes (as used in your code) are
not supported by Embedded Matlab. [1]

Therefore your solution unfortunatly won't work.

Thanks anyway,
Bernd



[1] http://www.mathworks.de/access/helpdesk/help/toolbox/eml/ug/bq37agh.html
From: Matt J on
Bernd <bernd.TA(a)gmx.net> wrote in message <996d4$4bfd5e03$d52f2ac2$19335(a)news.chello.at>...

>
> If required, i could define 'S' as the transposed Matrix of 'S'
=======

It would indeed be better to pre-transpose S. If you do, then there is the following:

St=S.';
S_p=zeros(n_cell,n_spec);

for ispec=1:n_spec
S_p(:,ispec) = r(:,:,ispec)*St(:,ispec) ;
end
From: Bruno Luong on
An alternative single-line solution is:

sum(bsxfun(@times,permute(r,[1 3 2]),permute(S,[3 1 2])),3)

The for loop might be still faster.

Bruno