Any ideas how i can reduce the execution time?

1 回表示 (過去 30 日間)
Dennis_Pana
Dennis_Pana 2016 年 7 月 19 日
回答済み: Dennis_Pana 2016 年 7 月 20 日
newdes=[0.6250 0.8750; 0.2500 0.7500; 0.3750 0.1250;
0.8750 0.3750; 0.1250 0.6250; 0 0; 0.5000 0.5000; 0.7500 0.2500;
0.6875 0.0625; 0.0625 0.8125; 0.1875 0.6875; 0.3125 0.3125;
0.4375 0.9375; 0.5625 0.5625; 0.9375 0.1875; 0.8125 0.4375;] ;
dis=zeros(v,1);
v=16;
m=2;
sum=0
s=2;
for j=1:v
for k=1:m
ddii=(newdes(s,k)-newdes(j,k))^2;%Distance calculation of point s at its new location with all the other points.
sum=sum+ddii;
end
dis(j,1)=sum; %Store all the distances.
sum=0;
end
I am trying to use bxsfun, but i cannot solve it. I want reduce the time effort. Thank you in advance.

採用された回答

Dennis_Pana
Dennis_Pana 2016 年 7 月 20 日
xx=bsxfun(@minus,newdes(s,:),newdes(1:v,:));
sbs=xx.^2;
dis(:,1)=sum(sbs,2);
That is what i was looking for.

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 7 月 19 日
newdes=[0.6250 0.8750; 0.2500 0.7500; 0.3750 0.1250;
0.8750 0.3750; 0.1250 0.6250; 0 0; 0.5000 0.5000; 0.7500 0.2500;
0.6875 0.0625; 0.0625 0.8125; 0.1875 0.6875; 0.3125 0.3125;
0.4375 0.9375; 0.5625 0.5625; 0.9375 0.1875; 0.8125 0.4375;] ;
[nn,mm]=size(newdes)
v=16;
m=2;
s=2;
ay=repmat(1:m,1,v)
ax=s*ones(1,numel(ay))
idx1=sub2ind([nn mm],ax',ay')
[xx,yy]=meshgrid(1:v,1:m)
idx2=sub2ind([nn mm],xx(:),yy(:))
ww=(newdes(idx1)-newdes(idx2)).^2 %Distance calculation of point s at its new location with all the other points.
dis1=sum(reshape(ww,2,[]))'
  5 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 7 月 19 日
編集済み: Azzi Abdelmalek 2016 年 7 月 19 日
I made some improvement, but it's less faster. Sometimes for loop is more efficient!
[nn,mm]=size(newdes);
s=2;
ay=repmat(1:mm,nn,1);
ax=s*ones(size(ay));
idx1=reshape(sub2ind([nn mm],ax',ay'),mm,[]);
[xx,yy]=meshgrid(1:v,1:m);
idx2=sub2ind([nn mm],xx,yy);
ww=(newdes(idx1)-newdes(idx2)).^2 ; %Distance calculation of point s at its new location with all the other points.
dis1=sum(ww)';
Dennis_Pana
Dennis_Pana 2016 年 7 月 19 日
Thank you for your effort. I will try to find a way to improve the time efficiency. Thanks again

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeNaNs についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by