MATLAB: Parfor: Variable (Ind) is not sliced; Recommendations about the code

parforsliced

I have the below lines of code and i want to make it with parfor. But i get the folloing message: Parfor cannot run due to the way Ind is used, and the it has minor corrections about the variables using the vector Ind that are not sliced. Any suggestions?
parfor iel=1:nelm
inadd=0;
Hnodes=Element_Table(iel,1:tot_Nods);
for il=1:z_nodes
for i_ind=1:tot_Nods;
hn=Hnodes(i_ind);
ielN=Element_Table(iel,i_ind+(il-1)*tot_Nods);
if (ielN>mxH && ielN<mnH);
DOFS=middofs; else DOFS=HalfDofs; end
[posf, poslf]=locF(il, z_nodes, totnodes, hn);
DoFs=1+inadd:DOFS+inadd; idofs=1:DOFS;
Ind(DoFs)=idofs+posf+poslf;
inadd=inadd+DOFS;
end
end
Unv(posel,1)=Un(Ind);
Ubv(posel,1)=Ub(Ind);
Kvn(Ind)=Kvn(Ind)+ K*Unv;
Mvn(Ind)=Mvn(Ind)+ M*Unv;
Mvb(Ind)=Mvb(Ind)+ M*Ubv;
end

Best Answer

Ind looks like a temporary variable and therefore needs to be created inside the parfor loop. Also, this kind of accumulation
Kvn(Ind)=Kvn(Ind)+ K*Unv;
Mvn(Ind)=Mvn(Ind)+ M*Unv;
Mvb(Ind)=Mvb(Ind)+ M*Ubv;
has no obvious parallel structure. Perhaps you meant the following
Kvn(el,Ind)= K*Unv;
Mvn(el,Ind)= M*Unv;
Mvb(el,Ind)= M*Ubv;
and then later after the parfor loop,
Kvn=squeeze(sum(Kvn,1));
Mvn=squeeze(sum(Mvn,1));
Mvb=squeeze(sum(Mvb,1));