Correctly indexing a parfor loop
2 views (last 30 days)
Show older comments
Hi all,
I converted a nested for loo into a unique parfor loop. The nested for loop looked like this:
for RIPETIZIONE = 1:N_RIPETIZIONI
for k=1:size(lambda_tol_vector)
%do things
end
end
Now, the parfor loop takes on the following values:
parfor n=1:N_RIPETIZIONI*K
hence merging into a single loop the nested loops of before.
Now, inside the "outer" nested loop (for RIPETIZIONE = 1:N_RIPETIZIONI), but outside the inner loop (for k=1:size(lambda_tol_vector)), there was this part:
scv=size(columns_validation_test,2);
for counter_columns = 1:scv
COLONNA_SELEZIONATA=columns_validation_test(counter_columns);
Ctest=zeros(size(A.Value));
Ctest(:,COLONNA_SELEZIONATA)=C(:,COLONNA_SELEZIONATA);
Cvalidation=C-Ctest;
for j=1:size(lambdavector)
RMSE_initial_test{counter_columns}(j)=sqrt(sum2(Diff_sq_initial{j}.*Ctest)/sum(Ctest(:)));
RMSE_final_corrected_validation{counter_columns}(j)=sqrt(sum2(Diff_sq_corrected{j}.*Cvalidation)/sum(Cvalidation(:)));
RMSE_final_test{counter_columns}(j)=sqrt(sum2(Diff_sq{j}.*Ctest)/sum(Ctest(:)));
RMSE_final_corrected_test{counter_columns}(j)=sqrt(sum2(Diff_sq_corrected{j}.*Ctest)/sum(Ctest(:)));
end
[~,arg_min_temp_1]=min(RMSE_final_corrected_validation{counter_columns});
end
which I need now to includde in the unique parfor loop with a pope indexing. Fo instance, Diff_sq_initial, Diff_sq_corrected and Diff_sq are all defined withing the parfor loop with an index of n in the new loop (whereas previosuly they were indexed with k inside the for k=1:size(lambda_tol_vector) loop).
Is there a way to properly indexing those variables so that they fit the new parfor loop?
Thaank you
1 Comment
Alvaro
on 24 Jan 2023
Edited: Alvaro
on 24 Jan 2023
If the last code block was between the outer and inner for loop, why would the variables be indexed by k if that is the index of the inner loop?
Also, please explain what is the end goal here, are you trying to parallelize your code? Combining your nested for loops into a parfor loop is not necessarily the optimal way of doing this.
Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!