I have a $N \times M$ list of lists, or rather, a list of records and I want to map a function over its columns (e.g. find the mean temperature, age, etc.). Map
would apply the function to each list within the list, but I want to slice vertically, rather than horizontally. How can I do this?
Example: Return a list of means for {{1, 4, 7}, {2, 5, 8}, { 3, 6, 9}}
rather than {1,2,3}
, etc.
The list:
{{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
Answer
Transpose
followed by mapping Mean
over the array should do the job.
data = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Mean /@ Transpose[data]
(* ==> {4, 5, 6} *)
Comments
Post a Comment