Improve the code's time of execution. I want to avoid the for loops and use element by element calculations. Do you guys have something in mind?? Thank you in advance.
Show older comments
for i=1:v
for j=1:v
for k=1:m
if dm==k && j<=n && A~=j
ddii=((design(i,k)-design(j,k))^2);
summ=summ+ddii;
elseif dm~=k && j<=n && A~=j
ddii=((design(A,k)-design(j,k))^2);
summ=summ+ddii;
end
k=k+1;j=j;i=i;
end
ddcc(j,i,:)=[j i summ];
j=j+1; i=i; k=1;summ=0;
end
cc(i,:)=min(nonzeros(ddcc(:,i,3)));
i=i+1;j=1;k=1;
end
4 Comments
Geoff Hayes
on 8 May 2016
Edited: Geoff Hayes
on 8 May 2016
Dennis - why, in your code, do you increment the i, j and k (indexing) variables? Is this intentional? Note that this is handled for you already by the step size (default of one) in the for loop so you never have to explicitly update them.
Your code would then reduce to
for i=1:v
for j=1:v
for k=1:m
if dm==k && j<=n && A~=j
ddii=((design(i,k)-design(j,k))^2);
summ=summ+ddii;
elseif dm~=k && j<=n && A~=j
ddii=((design(A,k)-design(j,k))^2);
summ=summ+ddii;
end
end
ddcc(j,i,:)=[j i summ];
summ=0;
end
cc(i,:)=min(nonzeros(ddcc(:,i,3)));
end
Have you considered pre-sizing your ddxx and cc arrays? That could help with performance depending upon what your v and m are.
Dennis_Pana
on 8 May 2016
dpb
on 8 May 2016
What're dm, n and A?
Dennis_Pana
on 8 May 2016
Edited: Dennis_Pana
on 8 May 2016
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!