Correctly indexing a parfor loop

4 ビュー (過去 30 日間)
federico nutarelli
federico nutarelli 2022 年 12 月 6 日
編集済み: Alvaro 2023 年 1 月 24 日
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 Center および File ExchangeLoops 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