如何向量化矢量函数的总和

时间:2015-04-02 19:12:38

标签: matlab vectorization

我在MATLAB中有一个for循环,它按如下方式计算正弦函数的总和:

% preliminary constants, etc.
tTot = 2;
fS = 10000;dt = 1/fS; % total time, sampling rate
Npts = tTot * fS; %number of points
t = dt:dt:tTot;
c1 = 2*pi/tTot;
c2 = pi/fS;    
s = zeros(1,Npts)

% loop to optimize:
for(k=1:Npts/2)
    s = s + sin(c1*k*t - c2*k*(k-1))
end

基本上,随着Npts变得很大,单循环for循环变得非常慢。困难在于我将参数k定义的矢量求和k

有没有办法通过矢量化来提高效率?我到目前为止采用的一种方法是定义一个矩阵并总结结果,但这给了我一个更大的向量的内存不足错误:

[K,T] = meshgrid(1:1:Npts,t);
s = sum(sin(c1*K.*T - c2*K.*(K-1)),2);

1 个答案:

答案 0 :(得分:3)

方法#1

使用差异公式的正弦: sin(A-B) = sin A cos B - cos A sin B ,使我们能够利用fast matrix multiplication -

K = 1:Npts/2;
p1  = bsxfun(@times,c1*K(:),t(:).');
p2 = c2*K(:).*(K(:)-1);
s = cos(p2).'*sin(p1) - sin(p2).'*cos(p1);

方法#2

使用bsxfun -

K = 1:Npts/2;
p1  = bsxfun(@times,c1*K(:),t(:).');
p2 = c2*K(:).*(K(:)-1);
s = sum(sin(bsxfun(@minus, p1,p2)),1);

可以修改方法#1以引入更小的循环以适应具有大数据阵列的问题,如下所示 -

num_blks = 100;%// Edit this based on how much RAM can handle workspace data
intv_len = Npts/(2*num_blks); %// Interval length based on number of blocks

KP = 1:Npts/2;
P2 = c2*KP(:).*(KP(:)-1);
sin_P2 = sin(P2);
cos_P2 = cos(P2);

s = zeros(1,Npts);
for iter = 1:intv_len:Npts/2
    K = iter:iter+intv_len-1;
    p1  = bsxfun(@times,c1*K(:),t(:).');
    s = s + (cos_P2(K).'*sin(p1) - sin_P2(K).'*cos(p1));
end