Re: when dimension increases
- To: mathgroup at smc.vnet.net
- Subject: [mg67577] Re: [mg67531] when dimension increases
- From: "Carl K. Woll" <carlw at wolfram.com>
- Date: Sat, 1 Jul 2006 05:12:07 -0400 (EDT)
- References: <200606300813.EAA27238@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
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
It's unclear to me what you are trying to do, but maybe the following
will help. There is an optional third argument to Map. Here is an
example list:
In[15]:= data = Table[Random[Integer, 10], {3}, {5}, {3}]
Out[15]= {
{{0, 2, 0}, {4, 8, 4}, {10, 1, 6}, {2, 8, 4}, {2, 10, 10}},
{{2, 7, 0}, {9, 7, 5}, {1, 0, 8}, {5, 7, 3}, {7, 6, 4}},
{{9, 4, 6}, {6, 3, 8}, {1, 7, 9}, {3, 9, 5}, {8, 7, 8}}
}
Suppose you just want to sort the inner lists of integer triples. Then:
In[16]:= Map[Sort, data, {-2}]
Out[16]= {
{{0, 0, 2}, {4, 4, 8}, {1, 6, 10}, {2, 4, 8}, {2, 10, 10}},
{{0, 2, 7}, {5, 7, 9}, {0, 1, 8}, {3, 5, 7}, {4, 6, 7}},
{{4, 6, 9}, {3, 6, 8}, {1, 7, 9}, {3, 5, 9}, {7, 8, 8}}
}
Suppose you want to sort the inner list of integer triples and the inner
list of 5 integer triples:
In[17]:= Map[Sort, data, {-3, -2}]
Out[17]= {
{{0, 0, 2}, {1, 6, 10}, {2, 4, 8}, {2, 10, 10}, {4, 4, 8}},
{{0, 1, 8}, {0, 2, 7}, {3, 5, 7}, {4, 6, 7}, {5, 7, 9}},
{{1, 7, 9}, {3, 5, 9}, {3, 6, 8}, {4, 6, 9}, {7, 8, 8}}
}
If you are unsure how Sort is being applied to the data, you can always
replace it with a nonevaluating head, e.g.:
In[18]:= Map[sort, data, {2}]
Out[18]= {
{sort({0, 2, 0}), sort({4, 8, 4}), sort({10, 1, 6}), sort({2, 8, 4}),
sort({2, 10, 10})},
{sort({2, 7, 0}), sort({9, 7, 5}), sort({1, 0, 8}), sort({5, 7, 3}),
sort({7, 6, 4})},
{sort({9, 4, 6}), sort({6, 3, 8}), sort({1, 7, 9}), sort({3, 9, 5}),
sort({8, 7, 8})}
}
Carl Woll
Wolfram Research