【发布时间】:2019-03-19 05:02:14
【问题描述】:
相关代码的基本部分可以提炼成:
list=rand(1,x); % where x is some arbitrarily large integer
hitlist=[];
for n=1:1:x
if rand(1) < list(n)
hitlist=[hitlist n];
end
end
list(hitlist)=[];
这个程序运行得很慢,我怀疑这就是原因,但我不知道如何修复它。 hitlist 的长度必然会以随机方式变化,因此我不能简单地预先分配适当大小的“零”。我打算将hitlist 和zeros 设置为列表的长度,但随后我将不得不删除所有多余的零,而且我不知道如何做到这一点而不会遇到同样的问题。
如何预分配随机大小的数组?
【问题讨论】:
标签: arrays matlab pre-allocation