Correctly indexing a parfor loop

조회 수: 2 (최근 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 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