如何从 Octave 中的 pdist2 函数生成索引?

问题描述 投票:0回答:1

Matlab 函数作为

pdist2
函数,可以计算每行之间的距离,例如
L2-norm
。 GNU Octave 具有相同的功能
pdist2

这些函数之间的区别在于,Matlab 可以返回索引

[D, I] = pdist2(X, Y, 'norm')
,而 GNU Octave 只能返回距离
[D] = pdist2(X, Y, 'norm')

问题:

假设你有矩阵

X

X = [-0.2515    1.0451   -1.2817
     -1.9741    0.2782   -1.0234
     10.6772   10.7365    9.9264
      8.7785   11.1680    9.5915
    -34.2330  -30.8410  -31.7200
    -32.6290  -31.7860  -31.2900
     45.0000   43.0000         0
     23.0000   -3.0000    2.0000];

并且您正在使用

D = pdist2(X, X, "euclidean")
来表示欧氏距离

D = [0.0000000       1.9032127       18.4114208      17.3850403      55.6593018      55.0153084      61.7215919      23.8278294
     1.9032127       0.0000000       19.7314529      18.6247940      54.3260574      53.7019653      63.5040855      25.3691425
     18.4114208      19.7314529      0.0000000       1.9757286       74.0272751      73.3647156      48.1406441      20.0840893
     17.3850403      18.6247940      1.9757286       0.0000000       72.9478302      72.3251266      49.1657410      21.4619255
     55.6593018      54.3260574      74.0272751      72.9478302      0.0000000       1.9108551       112.8561935     72.0262222
     55.0153084      53.7019653      73.3647156      72.3251266      1.9108551       0.0000000       112.2420197     70.9326706
     61.7215919      63.5040855      48.1406441      49.1657410      112.8561935     112.2420197     0.0000000       51.0294037
     23.8278294      25.3691425      20.0840893      21.4619255      72.0262222      70.9326706      51.0294037      0.0000000];

如果我无法通过

I
函数访问,如何生成索引
pdist2

matlab indexing octave euclidean-distance pdist
1个回答
0
投票

可以尝试使用自定义功能

function nearest_indices = find_nearest_indices(D)
    % Find the indices of the nearest points for each point in the matrix.
    % Args:
    % D : A square matrix containing distances between points.
    % Returns:
    % An array of indices of the nearest points.
    % Usage:
    % nearest_indices = find_nearest_indices(D);

    % Number of rows in D
    n = size(D, 1);
    
    % Preallocate array for indices
    nearest_indices = zeros(n, 1);

    for i = 1:n
        % Sort the distances in the current row
        [sorted_distances, sorted_indices] = sort(D(i, :));

        % Find the index of the nearest neighbor
        % The first element in sorted_indices is the point itself, so we take the second element
        nearest_indices(i) = sorted_indices(2);
    end
end
© www.soinside.com 2019 - 2024. All rights reserved.