简体   繁体   English

加速这个Matlab代码?

[英]speed up this matlab code?

Is there a good way to speed up this block of matlab code ( n in particular, can be large) using matrix operations, or anything? 是否有一种使用矩阵运算或其他方法来加速此matlab代码块(尤其是n可能很大)的好方法? Over 1/4 of my execution time is in this small block of code. 我的执行时间中有超过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

Test code: 测试代码:

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?

You can skip the loop: 您可以跳过循环:

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

In situations where you can't do vectorized calculations as above, you should at least preallocate the output array 在无法进行上述矢量化计算的情况下,至少应预先分配输出数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM