6

Suppose I have a list like

{
 {x1,y1,z1},
 {x2,y2,z2},
 {x3,y3,z3},
 ...
}

and I want to change it to

{
 {x1+Cos[z1/x1],y1,z1-Sin[z1/x1]},
 {x2+Cos[z2/x2],y2,z2-Sin[z2/x2]},
 {x3+Cos[z3/x3],y3,z3-Sin[z3/x3]},
 ...
}

What is the best way to do these matrix manipulations?

Peter Mortensen
  • 759
  • 4
  • 7

5 Answers5

10

A simple way would be to define a function to transform a single triple:

transform[{x_, y_, z_}] := {x + Cos[z/x], y, z - Sin[z/x]}

and then map it over your list:

transform /@ list
lericr
  • 27,668
  • 1
  • 18
  • 64
9
{#1+Cos[#3/#1],#2,#3-Sin[#3/#1]}&@@@lst


(* {
    {x1 + Cos[z1/x1], y1, z1 - Sin[z1/x1]}, 
    {x2 + Cos[z2/x2], y2, z2 - Sin[z2/x2]}, 
    {x3 + Cos[z3/x3], y3, z3 - Sin[z3/x3]}
   } *)

In addition, using Inner:

Inner[Times, lst, {1,1,1},{#1+Cos[#3/#1], #2,#3-Sin[#3/#1]}&]==%

(* True *)

Or, using MapThread:

MapThread[{#1+Cos[#3/#1], #2,#3-Sin[#3/#1]}&,Transpose@lst]==%%

(* True *)


Just for fun: MapThread:

MapThread may be used to apply a function to each column of a matrix.

MapThread[f,lst]

(* { f[x1, x2, x3], f[y1, y2, y3], f[z1, z2, z3] } * )

(And unlike Thread, MapThread does not evaluate its argument before threading: see here)

But:

 MapThread[f@#&,lst]

(* {f[x1], f[y1], f[z1]} *)

MapThread[f@##&,lst]==MapThread[f,lst]

(* True *) 

Therefore:

MapThread[{f@#1,g@#2,h@#3}&,lst]

(* { {f[x1], g[x2], h[x3]}, {f[y1], g[y2], h[y3]}, {f[z1], g[z2], h[z3]} } *)

MapThread[{f@#1,g@#2,h@#3}&,Transpose@lst]

(* {
    {f[x1], g[y1], h[z1]},
    {f[x2], g[y2], h[z2]}, 
    {f[x3], g[y3], h[z3]}
    } *)

lst 

(* {
    {x1, y1, z1}, 
    {x2, y2, z2}, 
    {x3, y3, z3}
   } *) 
user1066
  • 17,923
  • 3
  • 31
  • 49
8

Equivalent to @lericr`s answer except a pure function is used rather than defining a helper function

$Version

(* "13.3.0 for Mac OS X ARM (64-bit) (June 3, 2023)" *)

Clear["Global`*"]

Using indexed variables formatted as subscripts

(Format[#[n_]] := Subscript[#, n]) & /@ {x, y, z};

list1 = Array[{x[#], y[#], z[#]} &, 4]

enter image description here

list2 = {#[[1]] + Cos[#[[3]]/#[[1]]], #[[2]], #[[3]] - Sin[#[[3]]/#[[1]]]} & /@
   list1

enter image description here

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
7

Another way using Cases:

lst = {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}};

Cases[lst, {s1_, s2_, s3_} :> {s1 + Cos[s3/s1], s2, s3 - Sin[s3/s1]}]

enter image description here

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44
4

Using Replace:

amat = {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}};
Replace[amat, {x_, y_, z_} -> {x + Cos[z/x], y, z - Sin[z/x]}, 1]

$$ \left( \begin{array}{ccc} \cos \left(\frac{\text{z1}}{\text{x1}}\right)+\text{x1} & \text{y1} & \text{z1}-\sin \left(\frac{\text{z1}}{\text{x1}}\right) \\ \cos \left(\frac{\text{z2}}{\text{x2}}\right)+\text{x2} & \text{y2} & \text{z2}-\sin \left(\frac{\text{z2}}{\text{x2}}\right) \\ \cos \left(\frac{\text{z3}}{\text{x3}}\right)+\text{x3} & \text{y3} & \text{z3}-\sin \left(\frac{\text{z3}}{\text{x3}}\right) \\ \end{array} \right) $$

Syed
  • 52,495
  • 4
  • 30
  • 85