필터 지우기
필터 지우기

Nested parfor and for-Loops gives error

조회 수: 3 (최근 30일)
Moein
Moein 2023년 10월 9일
편집: Shivam 2023년 10월 9일
Hi,
My code is as follows:
result1 = nan(totaal, 7);
tx_range=tx_min:stepsize_t:tx_max;
totaal_temp=1e6;
parfor t_x_index = 1:numel(tx_range)
result1_temp = zeros(totaal_temp, 7);
t_x = tx_range(t_x_index);
disp(['t_x = ', num2str(t_x)]);
% multiple for loops which does not interact with "t_x_index" or "t_x". So, this part is basically independent of the parfor variable.
% the results of the for loops are saved into a variable named "result1_temp".
% now, i want to store the "result1_temp" in "result1":
indexing = (1:size(result1_temp, 1)) + (t_x_index - 1) * size(result1_temp, 1);
result1(indexing, :) = result1_temp;
end
Matlab gives me an error and does not allow me to run this. The error says:
Matlab PARFOR loop cannot run due to the way variable "result1" is used. Could someone help with what could be wrong here?
Thanks.

채택된 답변

Shivam
Shivam 2023년 10월 9일
편집: Shivam 2023년 10월 9일
Hi Moein,
I understand that you are facing an error while indexing into "result1" inside PARFOR loop.
To address this, you can store the results in a temporary variable inside the PARFOR loop and then assign them to the appropriate section of "result1" outside the PARFOR loop.
You can refer the following workaround to work on changes:
% ...
parfor t_x_index = 1:numel(tx_range)
% ...
% ...
% Store the "result1_temp" in the temporary variable "result1_temp_combined"
result1_temp_combined{t_x_index} = result1_temp;
end
% Assign the results from "result1_temp_combined" to the appropriate section of "result1"
for t_x_index = 1:numel(tx_range)
indexing = (1:size(result1_temp_combined{t_x_index}, 1)) + (t_x_index - 1) * size(result1_temp_combined{t_x_index}, 1);
result1(indexing, :) = result1_temp_combined{t_x_index};
end
I hope it helps.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by