八度:如何对这个函数进行矢量化处理? [英] Octave: How can I vectorize this function?

查看:58
本文介绍了八度:如何对这个函数进行矢量化处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以将此功能的这些for循环矢量化吗?

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');

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

重点是,应将ij替换为在for块中为其分配的向量.

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

这篇关于八度:如何对这个函数进行矢量化处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆