Nested parfor and for-Loops gives error
1 回表示 (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
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
You can refer to the following documentation: https://in.mathworks.com/help/parallel-computing/troubleshoot-variables-in-parfor-loops.html
I hope it helps.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!