找出数字排列的差异并按结果排序

Finding the difference of the permutations of numbers and sorting by the result

我在一个数组中有几个数字,我想找出不同之处 在每个之间并按最低结果排序(我不想重复项目)。我尝试使用命令 "perms" 因为它获得了所有排列

v = [120;124;130];
p = perms(v)

但它似乎并不像我希望的那样工作。大家还有什么建议吗

示例: 我有 3 个数字 a=[120,124,130](请注意可能有数百个数字),它会找到数字之间的差异,然后按结果排序。计算结果如下文所示。

124-120 =4
130-124 =6
130-120 =10

所以最终的数组 b 看起来像下面的数组

b=
    [124 120 4
    130 124 6
    130 120 10]

PS:我使用的是 octave 3.8.1,它类似于 matlab

我们可以使用 PDIST 函数来计算成对距离,然后使用 ndgridtril 的组合来获取向量原始值的索引。最后我们根据距离排序using:

v = [120;124;130];
D = pdist(v, 'cityblock');
[a,b] = ndgrid(1:numel(v), 1:numel(v));
out = sortrows([v(nonzeros(tril(a,-1))) v(nonzeros(tril(b,-1))) D(:)], 3)

对于那些无法加载统计工具​​箱的人感谢@Amro

v = [120;124.6;130];
%taken out from pdist.m from statistics package
order = nchoosek(1:rows(v),2);
Xi = order(:,1);
Yi = order(:,2);
X = v';
d = X(:,Xi) - X(:,Yi);
y = norm (d, "cols");

[a,b] = ndgrid(1:numel(v), 1:numel(v));
out = sortrows([v(nonzeros(tril(a,-1))) v(nonzeros(tril(b,-1))) y(:)], 3)

out=
    124.6000   120.0000     4.6000
       130.0000   124.6000     5.4000
       130.0000   120.0000    10.0000