question about Parfor loop

I am writing to ask if you could help me with the parfor loop. My present codes in Matlab are nested “for loops” that consume a large amount of processing time, so I was hoping to convert the codes to allow for parallel computing. Here is the simplified version of my code (which is not presently compatible with parallel computing).
A = zeros(4, 11);
B = zeros(4, 11);
parfor i = 1:4
for j = 1:11
A(i, j+1) =A(i,j)+ B(i,j);
end
end
where A & B are vector data sets (in my actual code)
This code can be hypothetically run on four different computers simultaneously for each “i” value, by computing only the inner “for loop” on each individual computer. So I wonder if we could modify the above code in order to utilize the independent actual processing units (cores) in a single computer. Further, we believe our code requires that A(i, j+1) be dependent on A(i,j) .
Thank you for your time and assistance!

Answers (1)

A = zeros(4, 11);
B = zeros(4, 11);
parfor i = 1:4
Ai = A(i,:);
Bi = B(i,:);
for j = 1:11
Ai(j+1) = Ai(j) + Bi(j);
end
A(i,:) = Ai;
end
In the particular case where you promise that A(i,:) = 0, then
parfor i = 1 : 4
Ai = cumsum(B(i,:));
A(i,:) = Ai;
end
If you do not promise that A(i,1) = 0 then
parfor i = 1 : 4
Ai = cumsum([A(i,1); B(i,:)]);
A(i,:) = Ai(2:end);
end

Categories

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

Asked:

on 26 Aug 2015

Answered:

on 26 Aug 2015

Community Treasure Hunt

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

Start Hunting!