vectorisation a for loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Johnny
am 8 Dez. 2019
Kommentiert: Stephen23
am 8 Dez. 2019
c=5;
retint=0;
dist=log([10:-1:1]+retint);
for i=1:length(dist)
eta=exp(-c*abs(dist(i)-dist));
discrim(i)=1/sum(eta);
end
Does anyone know how to vectorise this for loop to make it more efficient?
1 Kommentar
Stephen23
am 8 Dez. 2019
Because you are not concatenating anything, square brackets are not needed here:
dist=log((10:-1:1)+retint);
Akzeptierte Antwort
David Hill
am 8 Dez. 2019
Although arrayfun is really a loop,
c=5;
retint=0;
dist=log([10:-1:1]+retint);
discrim=arrayfun(@(x)1/sum(exp(-c*abs(dist-x))),dist);
0 Kommentare
Weitere Antworten (1)
Stephen23
am 8 Dez. 2019
Real vectorized code (no loop or arrayfun):
eta = exp(-c*abs(bsxfun(@minus,dist,dist(:))));
discrim = 1./sum(eta,1)
Or for MATLAB versions >=R2016b:
eta = exp(-c*abs(dist-dist(:)));
discrim2 = 1./sum(eta,1)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!