This might work, but two clarifying points
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