Correctly indexing a parfor loop

2 次查看(过去 30 天)
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 个评论
Alvaro
Alvaro 2023-1-24
编辑:Alvaro 2023-1-24
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by