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.

조회 수: 2 (최근 30일)
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
Dennis_Pana
Dennis_Pana 2016년 5월 8일
편집: Dennis_Pana 2016년 5월 8일
All this variables are integers, for example dm stands for dimensions, n number of rows and A is an integer too. All the integers are greater or equal to 1.

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

채택된 답변

dpb
dpb 2016년 5월 8일
See if the following produces correct output, first; should reduce the time slightly by eliminating redundant tests and shortening the loop on j. Whether it's significant will depend how many cases get missed so is data-dependent.
for i=1:v
for j=1:n
if j==A, continue, end
for k=1:n
if k==dm
ddii=((design(i,k)-design(j,k))^2);
summ=summ+ddii;
else
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
After that, you're walking thru the arrays in the opposite storage as to sequential addressing; whether this is a major issue has to do with stride and cache size, etc., but might try using
design=design.';
before beginning the loop and see if it makes any difference in speed when the first index gets incremented sequentially in the innermost loop.
After that, I'd have to think more than I wish to expend on a Sunday afternoon to see if could do better on the actual computation altho it would seem with some consideration a logical addressing expression and pdist|pdist2 should be able to help.
  댓글 수: 3
Dennis_Pana
Dennis_Pana 2016년 5월 8일
I checked the code you sent me and it gives the right results. The execution time is the same or almost the same with what i have had before. Thank you for your effort.
I do not understand what you mean by using design=design.'; I tried to use it before the loops but there is a size problem during the calculations.
dpb
dpb 2016년 5월 8일
...
for k=1:n
if k==dm
ddii=((design(i,k)-design(j,k))^2);
...
Note that k in both references in the array is the column index and is the innermost loop. This means the stride between every access is size(design,1), not sequential if were design(k,i) instead. I had a brain cramp on the transpose operator by itself, though, you would need to reference the indices in the opposite order as well, and if the array isn't square as I figured was, then you have to rearrange the loop limits as well. But, the big idea is that should iterate over the leftmost array index first to process the array in sequential order.
Still think you should be able to do something with the pdistX functions and logical addressing, though...

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by