4
$\begingroup$

We have a nested matrix

m1 = {{{1, 2, 2, I}, {1, 1, -2, 1}, {3, 2, 2, -I}}, {{3, 4, 4, I}, {1, 1, 4, 1}, {3, 3, 4, -1}}};

we must replace the first number of each sub_list with the third number and vise versa and and achieve to m2 as:

m2 = {{{2, 2, 1, I}, {-2, 1, 1, 1}, {2, 2, 3, -I}}, {{4, 4, 3, I}, {4, 1, 1, 1}, {4, 3, 3, -1}}};

The shape of m1 and m2 are as below. We know that we can use of two loops with an IF condition but we don't think it is a good way. If Possible please let us know how can we obtain m2?

m1:

enter image description here

m2:

enter image description here

$\endgroup$
1

4 Answers 4

6
$\begingroup$

One way:

m2 = Apply[{#3, #2, #, #4} &, m1, {2}]

or another:

m2 = m1;
m2[[;; , ;; , {3, 1}]] = m2[[;; , ;; , {1, 3}]];
m2
$\endgroup$
6
  • $\begingroup$ Can I use of MapAt instead of Apply[ ,{2}]? $\endgroup$ Commented Jan 21, 2016 at 18:36
  • 2
    $\begingroup$ @Ackaran Apply is shorter because with Map you have to refer to #[[2]] instead of #2 etc. And you need Map not MapAt. $\endgroup$
    – Kuba
    Commented Jan 21, 2016 at 18:37
  • 1
    $\begingroup$ Damn, both of my favorite methods in one post. Nothing left for me to write. :^) $\endgroup$
    – Mr.Wizard
    Commented Jan 21, 2016 at 18:44
  • $\begingroup$ The second way is so intelligent, I enjoyed it. $\endgroup$ Commented Jan 21, 2016 at 18:44
  • $\begingroup$ @Ackaran See the question I just linked in a comment below your post for more examples like this. $\endgroup$
    – Mr.Wizard
    Commented Jan 21, 2016 at 18:45
3
$\begingroup$

"Monster voodoo machine" method for those who want MapAt, do not want to bother with Part and like Span:

MapAt[Replace[{a_, b_, c_, d_} -> {c, b, a, d}], m1, {;; , ;;}]
$\endgroup$
3
  • $\begingroup$ You can use Map[Replace[...], m1, {2}] too. $\endgroup$
    – Kuba
    Commented Jan 22, 2016 at 11:32
  • 1
    $\begingroup$ @Kuba, I see it, but the OP asked for MapAt. I've made it just for fun :))) $\endgroup$
    – garej
    Commented Jan 22, 2016 at 11:33
  • $\begingroup$ BTW, this Replace[m1, {a_, b_, c_, d_} -> {c, b, a, d}, {2}] also does the job. $\endgroup$
    – garej
    Commented Jan 23, 2016 at 9:02
2
$\begingroup$
 m = 
 {{{1, 2, 2, I}, {1, 1, -2, 1}, {3, 2, 2, -I}}, 
  {{3, 4, 4, I}, {1, 1, 4, 1}, {3, 3, 4, -1}}};

wanted = 
 {{{2, 2, 1, I}, {-2, 1, 1, 1}, {2, 2, 3, -I}}, 
  {{4, 4, 3, I}, {4, 1, 1, 1}, {4, 3, 3, -1}}};

Using ReplaceAll:

(m /. v_?VectorQ :> Reverse@RotateRight[v, 1]) === wanted

(*True*)
$\endgroup$
1
$\begingroup$
m = 
 {{{1, 2, 2, I}, {1, 1, -2, 1}, {3, 2, 2, -I}}, 
  {{3, 4, 4, I}, {1, 1, 4, 1}, {3, 3, 4, -1}}};

wanted = 
 {{{2, 2, 1, I}, {-2, 1, 1, 1}, {2, 2, 3, -I}}, 
  {{4, 4, 3, I}, {4, 1, 1, 1}, {4, 3, 3, -1}}};

Using SubsetMap (new in 12.0)

result = Map[SubsetMap[RotateRight, #, {1, 3}] &, m, {2}]

returns

 {{{2, 2, 1, I}, {-2, 1, 1, 1}, {2, 2, 3, -I}}, 
  {{4, 4, 3, I}, {4, 1, 1, 1}, {4, 3, 3, -1}}};

result == wanted

True

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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