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.

采纳的回答

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 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by