5

I use mathematica 9.0

I tried it often and researched here but i wasnt foundable I have a question about swaping the position of 4 lists in 1 one whole list

a={{{1,2,3,4},{1,2,4,2}},{{1,4,8,1},{1,9,8,2}}}

Now i want to change the second and third position! So i need something like

ares={{{1,3,2,4},{1,4,2,2}},{{1,8,4,1},{1,8,9,2}}}

Thank you very much for your support

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Mudy Fa
  • 197
  • 5

2 Answers2

12
a = {{{1, 2, 3, 4}, {1, 2, 4, 2}}, {{1, 4, 8, 1}, {1, 9, 8, 2}}};
ares = a[[All, All, {1, 3, 2, 4}]]

{{{1, 3, 2, 4}, {1, 4, 2, 2}}, {{1, 8, 4, 1}, {1, 8, 9, 2}}}

Coolwater
  • 20,257
  • 3
  • 35
  • 64
7
list = {{{1, 2, 3, 4}, {1, 2, 4, 2}}, {{1, 4, 8, 1}, {1, 9, 8, 2}}}

ares = list /. {a_, b_, c_, d_} :> {a, c, b, d}

{{{1, 3, 2, 4}, {1, 4, 2, 2}}, {{1, 8, 4, 1}, {1, 8, 9, 2}}}

Or

ares = Apply[{#1, #3, #2, #4}&, list, {2}];
eldo
  • 67,911
  • 5
  • 60
  • 168