MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Pure function in a pure function (again)


data=Array[a,{15,5}];

Table[data[[n,1]]=Random[Integer,{1,5}],{n,Length[data]}];

Needs["Statistics`DataManipulation`"];

f[x_] := Plus@@Column[Select[data, #[[1]] == x &], 2] ;

Given your description, presumably you meant

Plus@@f/@Range[3]

a[1, 2] + a[2, 2] + a[3, 2] + a[6, 2] + a[9, 2] + a[14, 2]

This can be done without a helper function (and without the add-on package) as

Plus@@Cases[data,x_?(MemberQ[Range[3],#[[1]]]&):>x[[2]]]

a[1, 2] + a[2, 2] + a[3, 2] + a[6, 2] + a[9, 2] + a[14, 2]

%==%%

True


Bob Hanlon

---- Skirmantas Janusonis <janusonis at psych.ucsb.edu> wrote: 
> Hello,
> 
> I've already asked this question, but I thought there was a better way to state it. If Dat is a 2D-table, the following picks out the rows whose first element is 1,2 or 3 and adds up their the second elements:
> 
> f[x_] := Plus @@ Column[Select[Dat, #[[1]] == x &], 2]
> f /@ Range[3]
> 
> Instead of f/@Range[3], I'd like to use a second pure function, something like this:
> 
> Map[Plus @@ Column[Select[Dat, #1[[1]] == #2 &], 2]&,Range[3]]
> 
> This line is incorrect, however. How do I fix it?
> 
> Thanks!
> 
> Skirmantas
> 


  • Prev by Date: Re: Re: Extracting terms from an equation
  • Next by Date: Re: Re: Extracting terms from an equation
  • Previous by thread: Re: Pure function in a pure function (again)
  • Next by thread: Re: Pure function in a pure function (again)