加快这个matlab代码?

时间:2011-11-16 20:30:13

标签: performance matlab optimization

有没有一种很好的方法来加速这个matlab代码块(特别是n,可能很大)使用矩阵运算,还是其他什么?超过1/4的执行时间是在这个小块代码中。

% Get the bin indexes that we will place the network in
bins = [];
for n=low_freq:0.5:high_freq;
    bins = [bins, (n-spec_start)/spec_bin_size+1];
end

测试代码:

spec_start=2400
spec_bin_size=0.5
low_freq = 2437
high_freq=2438

bins = [];
for n=low_freq:0.5:high_freq;
    bins = [bins, (n-spec_start)/spec_bin_size+1];
end

bins  % 75 76 77

bins = [];
bins = (low_freq:0.5:high_freq - spec_start)./spec_bin_size + 1;

bins  % empty?

1 个答案:

答案 0 :(得分:6)

你可以跳过循环:

bins = ((low_freq:0.5:high_freq) - spec_start)./spec_bin_size + 1;

如果您无法进行上述矢量化计算,则至少应该preallocate the output array