Error: The variable Agent in a parfor cannot be classified
1 次查看(过去 30 天)
显示 更早的评论
parfor ii = 1:Workspace.Number_of_Compartments
Comp(ii).Vehicles = zeros(length(Compartment(ii).Vehicles),2)
for jj = 1:length(Compartment(ii).Vehicles)
Veh_ID = Compartment(ii).Vehicles(jj);
for zz = 1:length(Agent(Veh_ID).Structure.Following_Compartment)
if (Agent(Veh_ID).Structure.Following_Compartment(zz) == ...
Agent(Veh_ID).Structure.Current_Compartment)
Agent(Veh_ID).Structure.Local_Task = [Agent(Veh_ID).Structure.Local_Task,...
Agent(Veh_ID).Structure.Global_Task_Zones(zz)];
else
cur_comp = Agent(Veh_ID).Structure.Current_Compartment;
fol_comp = Agent(Veh_ID).Structure.Following_Compartment(zz);
Find1 = find(Compartment(cur_comp).Following_Compartment == fol_comp);
% All elements of the Find1 row of the field Exite_Nodes of
% the Compartment(cur_comp)
Find2 = Compartment(cur_comp).Exite_Nodes(Find1,:);
Find2_1 = find(Find2 ~= 0);
Find3 = find(Compartment(cur_comp).Exite_Nodes_State(Find1,:) == 0);
Find4 = intersect(Find2_1,Find3);
Find4 = Find4(randi(length(Find4)));
Agent(Veh_ID).Structure.Local_Task = [Agent(Veh_ID).Structure.Local_Task,...
Find2(Find4)];
end
end
end
How to eliminate this error? I need to add data in the structure inside parfor loop.
0 个评论
回答(1 个)
Jemima Pulipati
2020-12-22
Hello,
From my understanding, you are using the 'Agent' variable for slicing as well as indexing and trying to assign a value to it. This error arises because once you index a variable you cannot use it anywhere else in the parfor loop.
The suggested workaround would be to use a temporary variable inside the for loops and one 'Agent' variable that gets assigned the required values in the main parfor loop. In this way the variable 'Agent' would be referenced only once in the entire parfor loop.
Some answers from the community which might be of relevance to you are:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!