Parallel program required to be fixed for last couple of lines due to extensive time-consuming for saving variables in 3 by 3 matrix

parfor i=1:TotNu
a=(fy(i)*eye(3))+(Cyabs(i)*([cosa(i);cosb(i);cosc(i)]*[cosa(i) cosb(i) cosc(i)]));
b=a-Czabs(i)*([cosa(i);cosb(i);cosc(i)]*[cosa0(i) cosb0(i) cosc0(i)]);
xsj=(a*SAp(:,i)+b*[0;0;1]);
Ex=[Ex;xsj];
RondF_RondEta=[a RondF_RondEta];
RondF_RondZeta=[b RondF_RondZeta];
issues are with
RondF_RondEta=[a RondF_RondEta];
RondF_RondZeta=[b RondF_RondZeta];
where takes huge amount of time for saving. I tried to slice variable, and use parfor etc. I am looking for a solution to resolve that?

2 Comments

How large is TotNu? Is this the only thing you are doing inside the parfor loop?
It looks like this is basically some nD outer products and matrix-vector multiplies which could be done e.g. with the bsxfun and times functions. It will take more memory to store intermediate results (hence my question about TotNu) using the method I have in mind, but may be cleaner and faster overall than the parfor you are currently doing (parfor seems like overkill for what you have shown).

Sign in to comment.

Answers (2)

If is more efficient if you allocate Ex, RondF_RondEta and RondF_RondZeta before the loop:
Ex = nan(1, TotNu);
RondF_RondEta = nan(1, TotNu);
RondF_RondZeta = nan(1, TotNu);
for i=1:TotNu
a = (fy(i)*eye(3))+(Cyabs(i)*([cosa(i);cosb(i);cosc(i)]*[cosa(i) cosb(i) cosc(i)]));
b = a-Czabs(i)*([cosa(i);cosb(i);cosc(i)]*[cosa0(i) cosb0(i) cosc0(i)]);
Ex(i) = (a*SAp(:,i)+b*[0;0;1]);
RondF_RondEta(i) = a;
RondF_RondZeta(i) = b;
end
E.g., using nD operations without parfor:
cosabc = [cosa(:)'; cosb(:)'; cosc(:)'];
cos0abc = [cosa0(:)'; cosb0(:)'; cosc0(:)'];
a = bsxfun(@times,reshape(cosabc,3,1,TotNu),reshape(cosabc,1,3,TotNu));
a = bsxfun(@times,a,reshape(Cyabs,1,1,TotNu));
a = a + bsxfun(@times,eye(3),reshape(fy,1,1,TotNu));
b = bsxfun(@times,reshape(cosabc,3,1,TotNu),reshape(cos0abc,1,3,TotNu));
b = bsxfun(@times,b,reshape(Czabs,1,1,TotNu));
b = a - b;
Ex = zeros(3,TotNu);
for k=1:TotNu
Ex(:,k) = a(:,:,k) * SAp(:,k) + b(:,3,k); % nD matrix multiply
end
RondF_RondEta = reshape(a(:,:,TotNu:-1:1),3,[]);
RondF_RondZeta = reshape(b(:,:,TotNu:-1:1),3,[]);
Ex = Ex(:);

1 Comment

That sounds good since I realized calculation time is shortened. However, I intend to add matrix "a" and "b" like as
A(:,:,1)=a(:,:,1)+a(:,:,2)+......a(:,:,28)
A(:,:,2)=a(:,:,29)+a(:,:,302)+......a(:,:,56)
.
.
.
.
A(:,:,m1)=a(:,:,.........
and similarly for b
B(:,:,1)=b(:,:,1)+b(:,:,2)+......b(:,:,28)
B(:,:,2)=b(:,:,29)+b(:,:,302)+......b(:,:,56)
.
.
.
.
B(:,:,m1)=b(:,:,.........
so It seems I need to apply changes like this following
for k=1:m1
Ex(:,k) = A(:,:,k) * SAp(:,k) + B(:,3,k); % nD matrix multiply
end
TotNu=28*m1
I am looking for a way to do this summation but NOT WITH A LOOP! please let me know if you have any ideas.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 29 Jun 2015

Edited:

on 3 Jul 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!