Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pair arrays elements with min difference matlab

Tags:

arrays

matlab

I have two arrays 1x4 'x' and 'y'. I want to find which 'combination of elements 'paired' between these two arrays' will give minimum difference(elements of array are angles). I want to find WHICH elements should be paired to get that minimum. I do not care for the result minimum itself. I tried using indexing but didn't get anywhere.

Example:

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

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

xyMin=  [ 95    80   140    52];

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

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

EDITED:

I want to clarify that I want to find WHICH element of 'x' to pair with which element of 'y' so that difference between angles is minimum . i.e. x[1 2 3 4]-y[1 2 3 4] will give minimum. If there are more than one combinations that give same minimum choose first.

SORRY I REALIZE IT IS CONFUSING! thank a lot for help!

like image 950
mil Avatar asked Mar 25 '26 03:03

mil


1 Answers

This is basically RobertSettlers solution, but using the distance metric which is now clear from the discussion, a simple brute force approac:

x=x(:);
y=y(:);
Y=perms(y);
[distance,I]=min(sum(bsxfun(absDiffDeg,x,Y.'),1));
best_permuted_y=Y(I,:);
like image 196
Daniel Avatar answered Mar 26 '26 16:03

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!