List operations "in a given dimension"
- To: mathgroup at smc.vnet.net
- Subject: [mg33441] List operations "in a given dimension"
- From: Martin Johansson <martin.n.johansson at emw.ericsson.se>
- Date: Thu, 21 Mar 2002 09:27:28 -0500 (EST)
- Organization: Ericsson Microwave Systems AB
- Sender: owner-wri-mathgroup at wolfram.com
Hi,
coming from the matlab world, I am used to operating
on subgroups of elements in arbitrary dimensions of
multidimensional matrices. An example of such an
operation would be the calculation of the maximum
value over all elements in a particular dimension,
i.e. apply max() on all elements in the desired
dimension for all index combinations in the remaining
dimensions, resulting in matrix with one less
dimension than the original matrix.
Is there a "neat" (built-in) way to do this in
Mathematica?
Numerical example (2D):
aa = {{11,12,13},{21,22,23}};
Operating on aa with Max[] "w.r.t. the second
index" should yield
{13,23}.
Numerical example (3D):
aaa =
{{{111, 112, 113, 114}, {121, 122, 123, 124}, {131, 132, 133, 134}},
{{211, 212, 213, 214}, {221, 222, 223, 224}, {231, 232, 233, 234}}}
Again, operating on aa with Max[] "w.r.t. the second
index" should yield
{{131, 132, 133, 134}, {231, 232, 233, 234}}.
A way of doing (which I think is correct) is as
follows:
MaxInDim[list_, maxdim_] :=
Module[
{dims = Length[Dimensions[list]],
dimList},
dimList = Insert[Range[dims - 1], dims, maxdim];
Apply[Max, Transpose[list, dimList], {dims - 1}]
]
It seems to work, but if there is a "general" way of
doing this I could maybe avoid having to construct
similar versions for other common operations.
TIA for any ideas about this! Pointers to info on
Matlab->Mathematica syntax conversions would
also be welcome.
Ciao,
Martin