Re: when dimension increases
- To: mathgroup at smc.vnet.net
- Subject: [mg67572] Re: [mg67531] when dimension increases
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Sat, 1 Jul 2006 05:11:56 -0400 (EDT)
- References: <200606300813.EAA27238@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 30 Jun 2006, at 17:13, Arkadiusz Majka wrote:
> Hi,
>
> Imagine that I want to sort (or do anything else) a list
>
> list={5,2,8,3}
>
> So I use Sort[list]
>
> Next I add next dimension and obtain a list listNew=Table[list,{5}]
>
> In order to sort all sublists of it it I use Map[Sort, listNew]
>
> Now I add another more dimension listNew1=Table[list, {3},{5}]
>
> I can again sort all sublists of it using combination of Table and
> Map.
>
> The question is the following:
>
> How can I deal with expresions of unknown a priori dimension? For
> example what is the most elegant (clear and fast) method of sorting
> all
> sublists of multidimensional expresion? I would like to avoid Table
> and
> unclear expresions with many "Maps" (one embeded in another).
>
> Thanks for your help,
>
> Arek
>
I am not completely sure if I understand you correctly, but maybe
this might help. Suppose we have a list:
ls = {{2, 5, 4}, {{4, 3, 5}, {5, 1, 3}}}
And would like to Sort all the sublists. One approach would be like
this. Define an auxiliary function f:
f[l_List]:=Sort[l]
f[l_]:=l
and then simply use MapAll:
MalAll[f,ls]
{{{1, 3, 5}, {3, 4, 5}}, {2, 4, 5}}
It may be however that you only want to sort the "inner lists' only.
There are several possible approaches. One is simply to Map Sort on
the -2 level of ls:
Map[Sort,ls,{-2}]
{{2, 4, 5}, {{3, 4, 5}, {1, 3, 5}}}
Or, you could define a function g as follows:
g[l_?VectorQ]:=Sort[l]
g[l_]:=l
and again use MapAll:
MapAll[g,ls]
{{2, 4, 5}, {{3, 4, 5}, {1, 3, 5}}}
There are also several other similar methods I could suggest. It
would help in choosing the best one if I knew what exactly you wanted
to sort.
Andrzej Kozlowski
Tokyo, Japan