简体   繁体   English

MATLAB中复杂向量的速度有效分类

[英]Speed-efficient classification for complex vectors in MATLAB

I am trying to optimize this piece of code and get rid of the nested loop implemented. 我正在尝试优化这段代码并摆脱实现的嵌套循环。 I am finding difficulties in applying a matrix to pdist function 我发现将矩阵应用于pdist函数时遇到了困难

For example, 1+j // -1+j // -1+j // -1-j are the initial points and i am trying to detect 0.5+0.7j to with point it belong by min distance approach . 例如,1 + j // -1 + j // -1 + j // -1-j是初始点,我试图通过最小距离逼近来检测0.5 + 0.7j。
any help is appreciated 任何帮助表示赞赏

function result = minDisDetector( newPoints, InitialPoints)
result = [];
for i=1:length(newPoints)
    minDistance = Inf;
    for j=1:length(InitialPoints)

        X = [real(newPoints(i)) imag(newPoints(i));real(InitialPoints(j)) imag(InitialPoints(j))];
        d = pdist(X,'euclidean');

        if d < minDistance
            minDistance = d;
            index = j;
        end
    end
    result = [result; InitialPoints(index)]; 
end     
end

You can use efficient euclidean distance calculation as listed in Speed-efficient classification in Matlab for a vectorized solution - 您可以使用Speed-efficient classification in Matlab中的Speed-efficient classification in Matlab列出的高效欧氏距离计算,用于vectorized solution -

%// Setup the input vectors of real and imaginary into Mx2 & Nx2 arrays
A = [real(InitialPoints) imag(InitialPoints)];
Bt = [real(newPoints).' ; imag(newPoints).'];

%// Calculate squared euclidean distances. This is one of the vectorized
%// variations of performing efficient euclidean distance calculation using 
%// matrix multiplication linked earlier in this post.
dists = [A.^2 ones(size(A)) -2*A ]*[ones(size(Bt)) ; Bt.^2 ; Bt];

%// Find min index for each Bt & extract corresponding elements from InitialPoints
[~,min_idx] = min(dists,[],1);
result_vectorized = InitialPoints(min_idx);

Quick runtime tests with newPoints as 400 x 1 & InitialPoints as 1000 x 1 : 使用newPoints作为400 x 1InitialPoints1000 x 1快速运行时测试:

-------------------- With Original Approach
Elapsed time is 1.299187 seconds.
-------------------- With Proposed Approach
Elapsed time is 0.000263 seconds.

The solution is very simple. 解决方案非常简单。 However you do need my cartprod.m function to generate a cartesian product. 但是,您确实需要我的cartprod.m函数来生成笛卡尔积。

First generate random complex data for each variable. 首先为每个变量生成随机复杂数据。

newPoints = exp(i * pi * rand(4,1));
InitialPoints = exp(i * pi * rand(100,1));

Generate the cartesian product of newPoints and InitialPoints using cartprod . 使用cartprod生成newPointsInitialPoints的笛卡尔积。

C = cartprod(newPoints,InitialPoints);

The difference of column 1 and column 2 is the distance in complex numbers. 第1列和第2列的差异是复数的距离。 Then abs will find the magnitude of the distance. 然后abs会找到距离的大小。

A = abs( C(:,1) - C(:,2) );

Since the cartesian product is generated so that it permutates newPoints variables first like this: 由于生成笛卡尔积,因此它首先置换newPoints变量,如下所示:

 1     1
 2     1
 3     1
 4     1
 1     2
 2     2
 ...

We need to reshape it and get the minimum using min to find the minimum distance. 我们需要reshape它并使用min来获得最小值以找到最小距离。 We need the transpose to find the min for each newPoints . 我们需要转置来为每个newPoints找到min。 Otherwise without the transpose, we will get the min for each InitialPoints . 否则没有转置,我们将获得每个InitialPoints

[m,i] = min( reshape( D, length(newPoints) , [] )' );

m gives you the min, while i gives you the indices. m给你min,而i给你指数。 If you need to get the minimum initialPoints , just use: 如果您需要获得最小的initialPoints ,只需使用:

result = initialPoints( mod(b-1,length(initialPoints) + 1 );

It is possible to eliminate the nested loop by introducing element-wise operations using the euclidean norm for calculating the distance as shown below. 通过使用欧几里德范数引入逐元素运算来计算距离,可以消除嵌套循环,如下所示。

    result = zeros(1,length(newPoints)); % initialize result vector
    for i=1:length(newPoints)
        dist = abs(newPoints(i)-InitialPoints); %calculate distances
        [value, index] =  min(dist);
        result(i) = InitialPoints(index);
    end

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

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