2
$\begingroup$

Okay,

I have a list of angles, call it A. How would I arrange them in a 2D m-by-n grid G such that the distances between each adjacent angle-element (row- and column-wise) is minimized?

G has as many elements as A, or more. I get to pick m and n. Duplicating elements from A is allowed, but every element from A must be used at least once.

Toy Example 1:

A = {0, 10, 10, 20, 20, 20, 20, 30, 30}

Put them in a 3x3 array:

G = {
 {0,  10,  20},
 {10. 20,  30},
 {20, 30,  20}
}

Toy Example 2 (list of 5 angles, put them in a 3x2 grid, so have to duplicate 1 element):

A = {0, 350, 10, 20, 30}

G = {
 {0,   10, 20},
 {350,  0, 30}
}

Hopefully that was clear enough? Let me know if it wasn't....

$\endgroup$
  • 1
    $\begingroup$ What do you mean with the distance between angles? Their difference? Btw, try to format the code in valid Mathematica syntax next time :). $\endgroup$ – Teake Nutma Jul 20 '14 at 12:40
  • $\begingroup$ How about this: Fix $m=1$ and sort the entries of $A$. $\endgroup$ – Rahul Jul 20 '14 at 12:44
1
$\begingroup$

This seems to work:

A = Sort@{10, 0, 10, 20, 20, 20, 20, 30, 30};

per = Permutations[A];

tot = Total /@ Abs[Differences /@ per];

pos = First@Flatten@Position[tot, Length@A*10 - 10];

(for other spacings replace 10 by f.e. 5).

Partition[per[[pos]], 3]

{{0, 10, 20}, {10, 20, 30}, {20, 30, 20}}

For odd A:

B = Prepend[A, Min@A];

and repeat the above steps.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.