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.
Ältere Kommentare anzeigen
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 Kommentare
Geoff Hayes
am 8 Mai 2016
Bearbeitet: Geoff Hayes
am 8 Mai 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
am 8 Mai 2016
dpb
am 8 Mai 2016
What're dm, n and A?
Dennis_Pana
am 8 Mai 2016
Bearbeitet: Dennis_Pana
am 8 Mai 2016
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!