使用pdist2()返回矩阵中第二个最小值的索引

时间:2014-02-10 17:33:08

标签: matlab

我想使用pdist2返回第二个最小值的索引而不是值本身。例如:

% Compute the ordinary Euclidean distance
X = randn(100, 5);
Y = randn(25, 5);
D = pdist2(X,Y,'euclidean', 'smallest', 2); % euclidean distance

如何更改pdist2以使D包含索引而非值?

1 个答案:

答案 0 :(得分:1)

您只需添加第二个返回参数:

X = randn(100, 5);
Y = randn(25, 5);
[D I] = pdist2(X,Y,'euclidean', 'smallest', 2); % euclidean distance

I将包含索引。见http://www.mathworks.com/help/stats/pdist2.html