Optimize code without for loop
My code uses 6 nested for loops, it is basically used to build 3D matrix with data in specific order from 2D matrix with data in random order.
These lines work as expected but take significant times to execute.
Is there any alternative approach of programming which lead to avoid all these for loops and therefore speed up the execution ?
for z=1:1001
p=0;
for j=1:length(DOFNameRef)
for n=1:length(DOFDir)
for i=1:length(DOFNamePoint)
for l=1:length(DOFDir)
p = p+1;
for k=1:Mlig*Mcol
c(k) = contains(Resp(k),DOFNamePoint(i)) && contains(Resp(k),DOFDir(l)) && contains(Ref(k), DOFNameRef(j)) && contains(Ref(k),DOFDir(n));
end
[NumLigne, NumCol] = NLignNCol(p,Mlig, Mcol);
M(NumLigne,NumCol,z) = Data(z, c);
end
end
end
end
endMy code uses 6 nested for loops, it is basically used to build 3D matrix with data in specific order from 2D matrix with data in random order.
These lines work as expected but take significant times to execute.
Is there any alternative approach of programming which lead to avoid all these for loops and therefore speed up the execution ?
for z=1:1001
p=0;
for j=1:length(DOFNameRef)
for n=1:length(DOFDir)
for i=1:length(DOFNamePoint)
for l=1:length(DOFDir)
p = p+1;
for k=1:Mlig*Mcol
c(k) = contains(Resp(k),DOFNamePoint(i)) && contains(Resp(k),DOFDir(l)) && contains(Ref(k), DOFNameRef(j)) && contains(Ref(k),DOFDir(n));
end
[NumLigne, NumCol] = NLignNCol(p,Mlig, Mcol);
M(NumLigne,NumCol,z) = Data(z, c);
end
end
end
end
end My code uses 6 nested for loops, it is basically used to build 3D matrix with data in specific order from 2D matrix with data in random order.
These lines work as expected but take significant times to execute.
Is there any alternative approach of programming which lead to avoid all these for loops and therefore speed up the execution ?
for z=1:1001
p=0;
for j=1:length(DOFNameRef)
for n=1:length(DOFDir)
for i=1:length(DOFNamePoint)
for l=1:length(DOFDir)
p = p+1;
for k=1:Mlig*Mcol
c(k) = contains(Resp(k),DOFNamePoint(i)) && contains(Resp(k),DOFDir(l)) && contains(Ref(k), DOFNameRef(j)) && contains(Ref(k),DOFDir(n));
end
[NumLigne, NumCol] = NLignNCol(p,Mlig, Mcol);
M(NumLigne,NumCol,z) = Data(z, c);
end
end
end
end
end optimze, for loop MATLAB Answers — New Questions