Re: Getting stuck with finding an elegant solution without global variables
- To: mathgroup at smc.vnet.net
- Subject: [mg124830] Re: Getting stuck with finding an elegant solution without global variables
- From: Bob Hanlon <hanlonr357 at gmail.com>
- Date: Wed, 8 Feb 2012 05:34:19 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
Using Except would only work as intended when all of the elements were unique.
branch[list_?VectorQ] :=
list[[#]] + Drop[list, {#}] & /@ Range[Length[list]]
branch[Range[4]]
{{3, 4, 5}, {3, 5, 6}, {4, 5, 7}, {5, 6, 7}}
Bob Hanlon
On Tue, Feb 7, 2012 at 4:05 AM, Fredob <fredrik.doberl at gmail.com> wrote:
> Hi,
>
> I am trying to find an elegant solution, i.e. without a global
> variable to the following problem:
>
> Given a list, e.g. {1,2,3,4}, create a new list where each element is
> a function of the "i th" element and the rest of the list, e.g.
>
> Branch[Elem_, List_] := Elem + Cases[List, Except[Elem]]
>
> The the result would be {{3, 4, 5}, {3, 5, 6}, {4, 5, 7}, {5, 6, 7}}
>
> I have used Map[f, List] but then I have to use a global variable in f
> to access the list itself, e.g. BranchG[Elem_] := Elem + Cases[List,
> Except[Elem]]
>
--
Bob Hanlon