Trouble in rewriting the for loop as a parfor loop
Show older comments
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
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!