Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a list that is obtained by flattening a matrix

list=Flatten[Table[{i, j}, {i, 1, 4}, {j, 2, 6, 2}], 1]

{{1, 2}, {1, 4}, {1, 6}, {2, 2}, {2, 4}, {2, 6}, {3, 2}, {3, 4}, {3, 6}, {4, 2}, {4, 4}, {4, 6}}

And I am looking for at way to reverse all "rows" with even numbers (the first element).

So that my list will look like this

{{1,2}, {1,4}, {1,6}, {2,6}, {2,4}, {2,2}, {3,2}, {3,4}, {3,6}, {4,6}, {4, 4}, {4, 2}} 

Note that the first element stays the same, but the last element (for list with even first element) for the whole "row" has reversed.

Maybe there is an easy solutions to this, but I have not been able to find it yet.

share|improve this question

6 Answers 6

up vote 4 down vote accepted

Try this

list = {{1, 2}, {1, 4}, {1, 6}, {2, 2}, {2, 4}, {2, 6}, {3, 2}, {3, 4}, 
        {3, 6}, {4, 2}, {4, 4}, {4, 6}};
Join @@ MapAt[Reverse, #, Position[#, {{x_?EvenQ, _}, ___}]] &@ GatherBy[list, First]

{{1, 2}, {1, 4}, {1, 6}, {2, 6}, {2, 4}, {2, 2}, {3, 2}, {3, 4}, {3, 6}, {4, 6}, {4, 4}, {4, 2}}

It will be simpler if you store your list as

list2 = {{2, 4, 6}, {2, 4, 6}, {2, 4, 6}, {2, 4, 6}}

where positions is the number of "row"

MapAt[Reverse, list2, 2 ;; ;; 2]

{{2, 4, 6}, {6, 4, 2}, {2, 4, 6}, {6, 4, 2}}

share|improve this answer
    
e.g. with Partition[list[[All, 2]], 3] ... –  Yves Klett Nov 27 '13 at 19:00
    
Thanks. It works just fine :) The reason why the list is stored that way, is because i extracted it from a image (to one's and zeros), and used Position to extract coordinates for every one's. Thank you very much for the help. Now it's possible to make my robot draw both ways, and then minimizing the airtraveling time –  Punchline Nov 27 '13 at 19:14

One other solution (less tricky IMO):

list = Flatten[Table[{i, j}, {i, 1, 4}, {j, 2, 6, 2}], 1];
splittedlist = SplitBy[list, EvenQ]

{{{1, 2}, {1, 4}, {1, 6}}, {{2, 2}, {2, 4}, {2, 6}}, {{3, 2}, {3,4}, {3, 6}}, {{4, 2}, {4, 4}, {4, 6}}}

Flatten[Join[splittedlist[[#1]],Reverse@splittedlist[[#2]]] & @@@
  Partition[Range@Length@splittedlist, 2],1]

{{1, 2}, {1, 4}, {1, 6}, {2, 6}, {2, 4}, {2, 2}, {3, 2}, {3, 4}, {3, 6}, {4, 6}, {4, 4}, {4, 2}}

share|improve this answer
a= {{1, 2}, {1, 4}, {1, 6}, {2, 2}, {2, 4}, {2, 6}, {3, 2}, {3, 4}, {3, 6}, {4, 2}, 
   {4, 4}, {4, 6}}

A minor variation of ybeltukov's approach:

Flatten[If[EvenQ[#[[1, 1]]], Reverse[#], #] & /@ GatherBy[a, First],1]

Or this:

Join@@(GatherBy[a, First] /. {{x_?EvenQ, _}, z___} :> Append[Reverse[{z}], x])
share|improve this answer
Quiet[Flatten[MapIndexed[{#1, #2} &, GatherBy[list, First], {1}] /. {a_List, b_List} /; 
      EvenQ[b /. List -> Sequence] -> {a // Reverse, b} //. {a_List,b_List} -> a, 1]]

{{1, 2}, {1, 4}, {1, 6}, {2, 6}, {2, 4}, {2, 2}, {3, 2}, {3, 4}, {3, 6}, {4, 6}, {4, 4}, {4, 2}}

share|improve this answer

Perhaps:

fun[u_?(EvenQ[#[[1, 1]]] &)] := Reverse@u;
fun[u_] := u;
g[u_] := Flatten[fun /@ GatherBy[u, First@# &], 1];

or

fun[u_?(EvenQ[#[[1, 1]]] &)] := Reverse@u;
fun[u_] := u;
h[u_] := Join@@(fun /@ GatherBy[u, First@# &]);

Applying to test list:

list = {{1, 2}, {1, 4}, {1, 6}, {2, 2}, {2, 4}, {2, 6}, {3, 2}, {3, 
   4}, {3, 6}, {4, 2}, {4, 4}, {4, 6}}
g[list]

yields:

{{1, 2}, {1, 4}, {1, 6}, {2, 6}, {2, 4}, {2, 2}, {3, 2}, {3, 4}, {3,
6}, {4, 6}, {4, 4}, {4, 2}}

share|improve this answer

Perhaps something different:

MapIndexed[RotationMatrix[#2[[1]]π - π, {1, 1, 1}].#&, GatherBy[list, First]] ~Flatten~ 1
(* {{1,2}, {1,4}, {1,6}, {2,6}, {2,4}, {2,2}, 
    {3,2}, {3,4}, {3,6}, {4,6}, {4, 4}, {4, 2}} *)

As written, this works only for $3\times n$ matrices (flattened out). However, it can be generalized to $m\times n$ matrices by implementing a rotation matrix function for dimensions $>3$.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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