是否可以在Matlab中对程序操作(for/if)进行矢量化?

是否可以在Matlab中对程序操作(for/if)进行矢量化?,matlab,Matlab,我想知道下面的代码是否可以矢量化?或者,更直接地说,我尝试将一个数字与几个间隔相匹配,其结果决定增量过程的更新。非常感谢 pop_matrix = [10 0 0 0 0 0]; rand_num =rand; divid = [0.05 0.05 0.1 0.2 0.1 0.1]; for i = 1:6 if rand_num < sum(divid(1:i)) pop_matrix(i) = pop_matrix(i)+1; br

我想知道下面的代码是否可以矢量化?或者,更直接地说,我尝试将一个数字与几个间隔相匹配,其结果决定增量过程的更新。非常感谢

pop_matrix = [10 0 0 0 0 0];
    rand_num =rand;
    divid = [0.05 0.05 0.1 0.2 0.1 0.1];

for i = 1:6
    if rand_num < sum(divid(1:i))
       pop_matrix(i) = pop_matrix(i)+1;
       break
    end
end
pop_矩阵=[10 0 0];
rand_num=兰德;
divid=[0.05 0.05 0.1 0.2 0.1 0.1];
对于i=1:6
如果rand_num
以下各项应起作用:

pop_matrix = [10 0 0 0 0 0];
rand_num =rand;
divid = [0.05 0.05 0.1 0.2 0.1 0.1];

idx = find(cumsum(divid) > rand_num,1);
pop_matrix(idx) = pop_matrix(idx) + 1;

EDIT:一种使用
interp1
的方法,假设您想从名为
divid
的分发中提取
N
样本,速度大约快10倍:

pop_matrix = [10 0 0 0 0 0];
divid = [0.05 0.05 0.1 0.2 0.1 0.1];

N = 1000;               %// number of random samples to take
rand_num = rand(N,1);   %// generate N random numbers
dcs = cumsum(divid);    %// get cumulative distribution
dcs = dcs/dcs(end);     %// ensure this is normalized to 1
dcs = [0,dcs];          %// put a zero in front to create a new bin

s = interp1(dcs, 1:length(dcs), rand_num, 'previous', NaN); %// draw samples
pop_matrix = pop_matrix + accumarray(s,1)';                 %'//add up samples together

此过程基本上是使用从概率分布中进行采样,概率分布由
divid
定义,其中
dcs
是分布的累积密度函数(CDF)。

以下应起作用:

pop_matrix = [10 0 0 0 0 0];
rand_num =rand;
divid = [0.05 0.05 0.1 0.2 0.1 0.1];

idx = find(cumsum(divid) > rand_num,1);
pop_matrix(idx) = pop_matrix(idx) + 1;

EDIT:一种使用
interp1
的方法,假设您想从名为
divid
的分发中提取
N
样本,速度大约快10倍:

pop_matrix = [10 0 0 0 0 0];
divid = [0.05 0.05 0.1 0.2 0.1 0.1];

N = 1000;               %// number of random samples to take
rand_num = rand(N,1);   %// generate N random numbers
dcs = cumsum(divid);    %// get cumulative distribution
dcs = dcs/dcs(end);     %// ensure this is normalized to 1
dcs = [0,dcs];          %// put a zero in front to create a new bin

s = interp1(dcs, 1:length(dcs), rand_num, 'previous', NaN); %// draw samples
pop_matrix = pop_matrix + accumarray(s,1)';                 %'//add up samples together

此过程基本上是使用从概率分布中进行采样,概率分布由
divid
定义,其中
dcs
是分布的累积密度函数(CDF)。

以下应起作用:

pop_matrix = [10 0 0 0 0 0];
rand_num =rand;
divid = [0.05 0.05 0.1 0.2 0.1 0.1];

idx = find(cumsum(divid) > rand_num,1);
pop_matrix(idx) = pop_matrix(idx) + 1;

EDIT:一种使用
interp1
的方法,假设您想从名为
divid
的分发中提取
N
样本,速度大约快10倍:

pop_matrix = [10 0 0 0 0 0];
divid = [0.05 0.05 0.1 0.2 0.1 0.1];

N = 1000;               %// number of random samples to take
rand_num = rand(N,1);   %// generate N random numbers
dcs = cumsum(divid);    %// get cumulative distribution
dcs = dcs/dcs(end);     %// ensure this is normalized to 1
dcs = [0,dcs];          %// put a zero in front to create a new bin

s = interp1(dcs, 1:length(dcs), rand_num, 'previous', NaN); %// draw samples
pop_matrix = pop_matrix + accumarray(s,1)';                 %'//add up samples together

此过程基本上是使用从概率分布中进行采样,概率分布由
divid
定义,其中
dcs
是分布的累积密度函数(CDF)。

以下应起作用:

pop_matrix = [10 0 0 0 0 0];
rand_num =rand;
divid = [0.05 0.05 0.1 0.2 0.1 0.1];

idx = find(cumsum(divid) > rand_num,1);
pop_matrix(idx) = pop_matrix(idx) + 1;

EDIT:一种使用
interp1
的方法,假设您想从名为
divid
的分发中提取
N
样本,速度大约快10倍:

pop_matrix = [10 0 0 0 0 0];
divid = [0.05 0.05 0.1 0.2 0.1 0.1];

N = 1000;               %// number of random samples to take
rand_num = rand(N,1);   %// generate N random numbers
dcs = cumsum(divid);    %// get cumulative distribution
dcs = dcs/dcs(end);     %// ensure this is normalized to 1
dcs = [0,dcs];          %// put a zero in front to create a new bin

s = interp1(dcs, 1:length(dcs), rand_num, 'previous', NaN); %// draw samples
pop_matrix = pop_matrix + accumarray(s,1)';                 %'//add up samples together


这个过程基本上是使用从概率分布中取样,概率分布由
divid
定义,其中
dcs
是分布的累积密度函数(CDF)。

我将用矢量化的代码给出答案,但是您能澄清一下“将一个数字与多个间隔匹配”是什么意思吗?我只是想知道是否有更简单的方法来做这件事。非常感谢你的快速回复。我现在在猜测我的答案。当你把
中断
放在那里时,你的意思是你只想检查成功的第一个间隔,但之后没有检查,对吗?@你的解决方案几乎是正确的。您只需要索引到正确的索引中:
idx=find(cumsum(divid)>rand\u num,1)
,然后在
pop\u matrix
@Divakar中找到正确的索引元素。我浏览了一下
break
语句,没有考虑它。我会用矢量化的代码发布一个答案,但是你能澄清一下你所说的“将一个数字与几个间隔匹配”是什么意思吗?我只是想知道是否有更简单的方法来做这件事。非常感谢你的快速回复。我现在在猜测我的答案。当你把
中断
放在那里时,你的意思是你只想检查成功的第一个间隔,但之后没有检查,对吗?@你的解决方案几乎是正确的。您只需要索引到正确的索引中:
idx=find(cumsum(divid)>rand\u num,1)
,然后在
pop\u matrix
@Divakar中找到正确的索引元素。我浏览了一下
break
语句,没有考虑它。我会用矢量化的代码发布一个答案,但是你能澄清一下你所说的“将一个数字与几个间隔匹配”是什么意思吗?我只是想知道是否有更简单的方法来做这件事。非常感谢你的快速回复。我现在在猜测我的答案。当你把
中断
放在那里时,你的意思是你只想检查成功的第一个间隔,但之后没有检查,对吗?@你的解决方案几乎是正确的。您只需要索引到正确的索引中:
idx=find(cumsum(divid)>rand\u num,1)
,然后在
pop\u matrix
@Divakar中找到正确的索引元素。我浏览了一下
break
语句,没有考虑它。我会用矢量化的代码发布一个答案,但是你能澄清一下你所说的“将一个数字与几个间隔匹配”是什么意思吗?我只是想知道是否有更简单的方法来做这件事。非常感谢你的快速回复。我现在在猜测我的答案。当你把
中断
放在那里时,你的意思是你只想检查成功的第一个间隔,但之后没有检查,对吗?@你的解决方案几乎是正确的。您只需要索引到正确的索引中:
idx=find(cumsum(divid)>rand\u num,1)
,然后在
pop\u matrix
@Divakar中找到正确的索引元素。我没想就浏览了
break
语句。@如果你打算用代码画很多样本,第二个答案应该比第一个答案快。@XiaoYi如果你打算用代码画很多样本,第二个答案应该比第一个答案快。@XiaoYi第二个答案应该比第一个答案快。@XiaoYi首先,如果您计划使用代码绘制多个样本。@小毅如果您计划使用代码绘制多个样本,第二个答案应该比第一个答案运行得更快。