So I previously posted on here with regards to some code given HERE. Thanks to the help of Jan Simons I have managed to eliminate one of the nested for loops to speed up the code pretty significantly. However, due to the dimensionality of the project and future projects I am working on I still need to optimise the code. I ran the profiler and the following block of code seems to be bottleneck:
Eps = zeros(nObs, 1);Nn = zeros(nObs, 1);nx1 = zeros(nObs, 1);ny1 = zeros(nObs, 1);nx2 = zeros(nObs, 1);ny2 = zeros(nObs, 1);for i = 1:nObs dxSample = dx(i, :); dxSample(i) = []; dySample = dy(i, :); dySample(i) = []; dzSample = dz(i, :); dzSample(i) = []; [EpsSample, NnSample] = sort(dzSample, 'ascend'); Eps(i) = EpsSample(k); Nn(i) = NnSample(k); nx1(i) = sum(dxSample < Eps(i)); ny1(i) = sum(dySample < Eps(i)); nx2(i) = sum(dxSample <= Eps(i)); ny2(i) = sum(dySample <= Eps(i)); end
Note that the k is typically quite low: between 3-10, nObs is around 4364. Is there any way of speeding this up? Maybe even eliminating the for loop altogether if possible? Any help is very much appreciated!
Best Answer