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


i have several numbers in array , find difference between each 1 , sort lowest result (i don't want repeat items). tried using command "perms" since gets permutations

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

but doesn't seem work way like. have other suggestions

example: have 3 numbers a=[120,124,130] (please note there hundreds of numbers) , find differences between numbers, sort result. calculations text below.

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

so final array b array below

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

ps: i'm using octave 3.8.1 matlab

we can use pdist function compute pair-wise distances, using combination of ndgrid , tril indices original values of vector. sort using according distances:

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) 

Comments