Clear Filters
Clear Filters

Trouble in rewriting the for loop as a parfor loop

1 view (last 30 days)
I want to reduce unnecessary calculations in the loop, and this is the sample code for "for" loop which reduce the number of calculations from 200 to 120 :
R_num = 20;
C_numhalf = 10;
row = 1:C_numhalf;
Ray = R_num/C_numhalf*2;
row_flip = flip(row);
a = zeros(R_num,C_numhalf);
for ii = 1:R_num
col = [row(1:ceil(ii/Ray)) flip(row_flip((1:ceil(ii/Ray))))];
for jj = col % 1:C_numhalf
a(ii,jj) = 1;
end
end
disp(a)
The problem is how to rewrite the "for" loop into "parfor" loop, the following code does not work properly and this is the help link:
R_num = 20;
C_numhalf = 10;
row = 1:C_numhalf;
Ray = R_num/C_numhalf*2;
row_flip = flip(row);
a = zeros(R_num,C_numhalf);
parfor ii = 1:R_num
col = [row(1:ceil(ii/Ray)) flip(row_flip((1:ceil(ii/Ray))))];
for jj = col
a(ii,jj) = 1;
end
end
disp(a)
I have poor knowledge of parallel computing and failed to find a solution to the problem. Any suggestions are helpful!
By the way, there is an error in the help link:

Accepted Answer

Raymond Norris
Raymond Norris on 3 Nov 2020
This might work, but two clarifying points
  • These aren't the same
for jj = col % 1:C_numhalf
In your example code, col might be the vector [1 10], which when used as the range in a for loop is not the same as
1:10. I'm assuming you want the vector [1 x] and not the vector 1:x.
  • Depending on how you initialize a, what I'm showing may or may not work (unless you really do want to work with the vector 1:x and not [1 x]). If it's with zeros, you don't need to call zeros again in unit_of_work (just because of the way scalar expansion works). If it's any other helper function (e.g. nan), then you'll need to preallocate it with the same helper function (e.g. tempa = nan(1,C_numhalf)). If a is assigned some other way, for example a = myfcn(R_num,C_numhalf), what I'm suggesting most likely won't work. Based on your response, we might be able to write it differently.
function chenguang
R_num = 20;
C_numhalf = 10;
row = 1:C_numhalf;
Ray = R_num/C_numhalf*2;
row_flip = flip(row);
a = zeros(R_num,C_numhalf);
parfor ii = 1:R_num
a(ii,:) = unit_of_work(row,ii,Ray,row_flip,C_numhalf);
end
disp(a)
function tempa = unit_of_work(row,ii,Ray,row_flip,C_numhalf)
col = [row(1:ceil(ii/Ray)) flip(row_flip((1:ceil(ii/Ray))))];
tempa = zeros(1,C_numhalf);
for jj = col
tempa(1,jj) = 1;
end
  1 Comment
Chenguang Yan
Chenguang Yan on 3 Nov 2020
  • I want the vector [1 x] and not the vector 1:x, when ii == 1.
  • I preallocate a with a = sym(zeros(R_num,C_numhalf)) actually. Calculating the element of matrix a involves symbolic computation, which is why I hope to use parfor to speed up the calculation.
Following the method you provided, I placed the inner for loop in the local function in my own code, and the outer parfor loop seems to work normally. Thank you very much for your answers.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!