5
$\begingroup$

Given

{{a, b}, {c, d}}

I want to get

{{c, b}, {a, d}}

with one simple expression. Is this possible?

| improve this question | | | | |
$\endgroup$
  • 1
    $\begingroup$ With m = {{a, b}, {c, d}}, use m[[;; , 1]] = m[[Dimensions[m][[1]] ;; 1 ;; -1, 1]]? $\endgroup$ – egwene sedai May 10 '19 at 14:35
8
$\begingroup$

You can use SubsetMap for this:

l = {{a, b}, {c, d}}

SubsetMap[Reverse, l, {All, 1}]

{{c, b}, {a, d}}

That is, Reverse and replace the list of elements comprised of the 1nth element of All rows of l.

There are bound to be many other solutions.

| improve this answer | | | | |
$\endgroup$
7
$\begingroup$

I suggest making use of function argument destructuring. The required munger can be written directly from the problem statement. No need to dig out arcane knowledge of list manipulation.

mung[{{a_, b_}, {c_, d_}}] := {{c, b}, {a, d}}
m = {{a, b}, {c, d}}; mung[m]

{{c, b}, {a, d}}

| improve this answer | | | | |
$\endgroup$
4
$\begingroup$
Diagonal /@ {Reverse[m], m}

{{c, b}, {a, d}}

Transpose @ MapAt[Reverse, Transpose[m], {1}]

{{c, b}, {a, d}}

| improve this answer | | | | |
$\endgroup$
3
$\begingroup$

Your question is tagged both "list-manipulation" and "matrices". Other answers have given answers treating this as a nested list, but if you want to do it with matrix operations, the two columns of the resulting matrix are {{0, 1}, {1, 0}}.{{a, b}, {c, d}}.{{1},{0}} and {{a, b}, {c, d}}.{{0},{1}}, so you can them combine those two column vectors into a matrix to get the answer.

| improve this answer | | | | |
$\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.