Matlab如何加快阵列访问速度

时间:2015-01-08 18:14:50

标签: arrays performance matlab optimization

我正在运行模拟,跟踪各个点的位移(大约1000-100,000,具体取决于几何)。在计算下一次加速时,我需要访问当前位移。 数据保存在一个非常大的2d数组(1000x2)中,可通过以下行访问:

eta = U2(connect(2:neib+1,i),:) - onearray*U2(i,:);

U2是位移数组,connect是填充了整数的2d数组(61x1000)。此行针对每个点运行并且是瓶颈,占总计算时间的约33%。

所以我的问题是如何加快这条线?我猜这种缓慢与访问数组中的这么多点有关,那么有没有办法减少访问时间?

编辑:neib是一个从9到61的整数,用于跟踪连接中的非零行。 i是迭代次数,从1到点数。 Onearray = 1(neib,1)预先分配以减少时间。

这是围绕它的整个循环:

for ii=1:nnodes     % #for each node (nodes are points)
    neib = connect(1,ii);
    onearray = ones(neib,1);
    eta= U2(connect(2:neib+1,ii),:) - onearray*U2(ii,:);   % #calculates stretch of bonds and subtracts stretch of node
    if eta == 0
        continue
    end
    xi=xiall(2:neib+1,:,ii);    % #gets vector distance for all horpts for node 'i' (2:total # of horpts+1,all,node)
    od=odall(2:neib+1,ii);     % #gets magnitude for all horpts for node 'i' (2:total # of horpts+1,all,node)

    cp=xi+eta;  % #adds stretch to (x-x') (cp is the current relative position vector from node to each bond)
    cd = sqrt(cp(:,1).^2 + cp(:,2).^2); % #cd (vector) is the new radii from node to horpts
    s=cd./od-1;     % #strain (by definition)

    f=C*s./cd*A;    % #calculates unit force density function
    UD(ii,:)=UD(ii,:)+sum([f f].*cp); % #adding force density function to equation of motion

end

0 个答案:

没有答案