简体   繁体   中英

Octave: How can I vectorize this function?

Can these for-loops of this function be vectorized?

function [sta]=bootstrap(data,N,p)
rand('state', sum(100*clock));
n=length(data);
n1=round(prctile(1:n,(100-p)/2));
n2=round(prctile(1:n,p/2+50));
for i=1:N
    choose=round(((n-1)*rand(1,n))+1);
    for j=n1:n2
        sample(j-n1+1,1)=data(choose(j));
    end
sta(i)=mean(sample);
end

Yes you can, try to replace your loop with the code below:

choose=round(((n-1)*rand(N,n))+1); sample(:,(n1:n2)-n1+1,1)=data(choose(:,n1:n2)); sta=mean(sample');

The point is you should replace i and j with the vectors you assigned for them in for block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM