加快这个matlab代码?

gny*_*his 0 optimization performance matlab

有没有一种很好的方法n可以使用矩阵运算加速这个matlab代码块(特别是可能很大)?超过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
Run Code Online (Sandbox Code Playgroud)

测试代码:

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?
Run Code Online (Sandbox Code Playgroud)

Jon*_*nas 6

你可以跳过循环:

bins = ((low_freq:0.5:high_freq) - spec_start)./spec_bin_size + 1;
Run Code Online (Sandbox Code Playgroud)

在如上所述无法进行矢量化计算的情况下,至少应该预先分配输出数组

  • 你打败了我30秒! (2认同)
  • 缺少方括号.怎么样:([low_freq:0.5:high_freq] - spec_start)./ spec_bin_size + 1 (2认同)