Pairs of arrays with minimal matlab difference

I have two arrays 1x4 'x' and 'y'. I want to find which combination of "pairs" elements between these two arrays will give the minimum difference (the elements of the array are the angles). I want to find WHICH elements must be paired to get this minimum. I do not care about the very minimum of the result. I tried using indexing but got nothing.

Example:

x=[x1 x2 x3 x4], y=[y1 y2 y3 y4].  
x=[ 295 10 25 18 ], y=[ 200   290   245   326]    

I get the minimum angle difference between x and y 'xyMin' from here: Calculating the absolute differences between the two angles

xyMin=  [ 95    80   140    52];

This is the minimum difference between angle elements from 2 arrays. However, I want to know which elements of the arrays were conjugated to give this minimum. So I need to get something like:
  [Example]

xyArrayElementsThatGiveMinCombination:  [x1-y3, x2-y4, x3-y1, x4-y2]. 

Edition:

, WHICH- "x" "y", . x [1 2 3 4] -y [1 2 3 4] . , , .

, , ! !

+3
3

RobertSettlers, , , :

x=x(:);
y=y(:);
Y=perms(y);
[distance,I]=min(sum(bsxfun(absDiffDeg,x,Y.'),1));
best_permuted_y=Y(I,:);
+1

( )

[v,i] = min(sum(abs(perms(y)-repmat(x, factorial(4), 1)), 2))

v min i min ()

: 10 ( ), 3 !

+1

This is another idea how to solve the problem. I am not entirely sure whether it always produces the correct result. It is based on the assumption:

An optimal solution would be to sort both x and y, and then rotate y.

If so, this solution is much better, but I'm not sure if this is true.

x=x(:);
y=y(:);
%Sort both vector to reduce the problem to the simplified case
[sorted_x,index_x]=sort(x);
[sorted_y,index_y]=sort(y);
distance=nan(1,numel(x));
%circular shift, try all combinations
for shift=1:numel(x)
    distance(shift)=sum(absDiffDeg(circshift(sorted_x,shift),sorted_y));
end
%get the best shift
[minimal_distance,shift]=min(distance);
%Now a solution is fond permuting both x and y, the permutations for x and y are:
%circshift(index_x,shift) and index_y
%but permuting x is unnessecary. Undo the permutation of x and keep the paris between x and y
[~,reverse_x]=sort(circshift(index_x,shift));
%Best permutation for y
y_permutation=index_y(reverse_x);
%permute y
y_permuted=y(y_permutation);
+1
source

All Articles