필터 지우기
필터 지우기

Vectorizing interdependent nested 4-for loop

조회 수: 1 (최근 30일)
ingstalam
ingstalam 2015년 1월 12일
편집: ingstalam 2015년 1월 12일
How to speed up this piece of code? How to vectorise this code? Is no other way possible?
for i1=1:num_agents % for all i
for rows=1:num_tasks*num_parts-1 % for all m_i,n_i in E_i
for ell=1:gL-G(rows,rows+1,i1) % for all ell
for i2=1:num_agents
xtemp = clearer;
% half-sum 1st part
for s = ell:gL%-G(rows,rows+1,i1)
if mod(rows,num_tasks)~=0
xtemp(i1,ceil(rows/num_tasks),mod(rows,num_tasks),s) = 1;
elseif mod(rows,num_tasks)==0
xtemp(i1,ceil(rows/num_tasks),num_tasks,s) = 1;
end
end
% half-sum 2nd part
for s = 1:ell+G(rows,rows+1,i1)-1
if s <= gL
if mod(rows,num_tasks)~=0
xtemp(i2,ceil(rows/num_tasks),mod(rows,num_tasks)+1,s) = 1;
elseif mod(rows,num_tasks)==0
xtemp(i2,ceil(rows/num_tasks)+1,1,s) = 1;
end
end
end
xtemp = xtemp(:);
Aineq(counter,:) = xtemp';
bineq(counter) = 1;
counter = counter + 1;
end
end
end
end

답변 (1개)

Rick Rosson
Rick Rosson 2015년 1월 12일
  1. Did you pre-allocate Aineq prior to the loop?
  2. Did you pre-allocate bineq prior to the loop?
  댓글 수: 2
ingstalam
ingstalam 2015년 1월 12일
Yup.
Aineq = spalloc(J*K+J*K+I*ELL+I*I*gL*(J*K-1),matwid,3*I*J*K*ELL+I*I*gL*(J*K-1)*(G_max)); % Allocate Aineq
bineq = zeros(J*K+J*K+I*ELL+I*I*gL*(J*K-1),1); % Allocate bineq
ingstalam
ingstalam 2015년 1월 12일
편집: ingstalam 2015년 1월 12일
In fact i used sparse matrix allocation to speed up the results.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by