优化嵌套循环以计算矩阵行的xcorr [英] Optimize nested for loop for calculating xcorr of matrix rows

查看:84
本文介绍了优化嵌套循环以计算矩阵行的xcorr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个嵌套循环,它们执行以下操作:

I have 2 nested loops which do the following:

  • 获取两行矩阵
  • 检查索引是否满足条件
  • 如果这样做,请计算两行之间的xcorr并将其放入新向量中
  • 找到子向量的最大值的索引,并用该值替换LAG矩阵的元素
  • Get two rows of a matrix
  • Check if indices meet a condition or not
  • If they do: calculate xcorr between the two rows and put it into new vector
  • Find the index of the maximum value of sub vector and replace element of LAG matrix with this value

我不知道如何通过向量化或其他方式来加快此代码的速度.

I dont know how I can speed this code up by vectorizing or otherwise.

b=size(data,1);
F=size(data,2);
LAG= zeros(b,b);  

for i=1:b
    for j=1:b
        if j>i
            x=data(i,:);
            y=data(j,:);
            d=xcorr(x,y);
            d=d(:,F:(2*F)-1); 
            [M,I] = max(d);
            LAG(i,j)=I-1;
            d=xcorr(y,x);
            d=d(:,F:(2*F)-1);
            [M,I] = max(d);
            LAG(j,i)=I-1;
        end
    end
end

推荐答案

首先,请注意浮点精度...

您在注释中提到您的数据包含整数0、1和2.因此,您希望双精度中完成的,因此似乎是一些浮点错误.此错误可能导致结果比整数值稍大或略小.

First, a note on floating point precision...

You mention in a comment that your data contains the integers 0, 1, and 2. You would therefore expect a cross-correlation to give integer results. However, since the calculation is being done in double-precision, there appears to be some floating-point error introduced. This error can cause the results to be ever so slightly larger or smaller than integer values.

由于您的计算涉及寻找最大值的位置, ,那么如果重复的最大整数值带有附加的精度误差,则结果可能会略有不同.例如,假设您期望值10为最大值,并出现在向量d的索引2和4中.您可能会以一种方式计算d并获得d(2) = 10d(4) = 10.00000000000001,并带有一些附加的精度误差.因此,最大值将位于索引4中.如果使用不同的方法来计算d,则可能会得到d(2) = 10d(4) = 9.99999999999999,并且错误方向相反,导致最大值位于索引中. 2.

Since your calculations involve looking for the location of the maxima, then you could get slightly different results if there are repeated maximal integer values with added precision errors. For example, let's say you expect the value 10 to be the maximum and appear in indices 2 and 4 of a vector d. You might calculate d one way and get d(2) = 10 and d(4) = 10.00000000000001, with some added precision error. The maximum would therefore be located in index 4. If you use a different method to calculate d, you might get d(2) = 10 and d(4) = 9.99999999999999, with the error going in the opposite direction, causing the maximum to be located in index 2.

解决方案?首先舍入您的互相关数据:

The solution? Round your cross-correlation data first:

d = round(xcorr(x, y));

这将消除浮点错误,并为您提供预期的整数结果.

This will eliminate the floating-point errors and give you the integer results you expect.

现在,进入实际解决方案...

Now, on to the actual solutions...


您可以将矩阵传递给 xcorr 将对每个成对的列组合执行互相关.使用此方法,您可以像这样完全放弃循环:

You can pass a matrix to xcorr and it will perform the cross-correlation for every pairwise combination of columns. Using this, you can forego your loops altogether like so:

d = round(xcorr(data.'));
[~, I] = max(d(F:(2*F)-1,:), [], 1);
LAG = reshape(I-1, b, b).';


对于上述解决方案,data的大小是有限制的,因为它将产生较大的中间变量和输出变量,这些变量可能超过

There are limits to how large data can be for the above solution, since it will produce large intermediate and output variables that can exceed the maximum array size available. In such a case for loops may be unavoidable, but you can improve upon the for-loop solution above. Specifically, you can compute the cross-correlation once for a pair (x, y), then just flip the result for the pair (y, x):

% Loop over rows:
for row = 1:b

  % Loop over upper matrix triangle:
  for col = (row+1):b

    % Cross-correlation for upper triangle:
    d = round(xcorr(data(row, :), data(col, :)));
    [~, I] = max(d(:, F:(2*F)-1));
    LAG(row, col) = I-1;

    % Cross-correlation for lower triangle:
    d = fliplr(d);
    [~, I] = max(d(:, F:(2*F)-1));
    LAG(col, row) = I-1;

  end

end

这篇关于优化嵌套循环以计算矩阵行的xcorr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆