Re: For to Map to Increase Speed
- To: mathgroup at smc.vnet.net
- Subject: [mg53248] Re: For to Map to Increase Speed
- From: Peter Pein <petsie at arcor.de>
- Date: Mon, 3 Jan 2005 04:29:24 -0500 (EST)
- References: <cr8e90$r7k$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Benedetto Bongiorno wrote:
> To MathGroup,
>
> Is there a Map routine that can shorten the computation time of data to
> data02?
>
> The variable data is a list of 1036 lists within which are lists of
> sublists of 1 or more, usually more.
>
> I cannot flatten data because I need to keep the groupings (1036) that was
> created by Split.
>
> Dimensions[data]
>
> {1036}
>
> data02={};
>
> n=Length[data]+1;
>
> For[i=1,i<n,i++,step1=Mean[ColumnTake[data[[i]],{4,21}]];
>
> AppendTo[data02,Join[data[[i,1,{1,2}]],step1]]]
>
> Dimensions[data02]
>
> {1036,20}
>
>
>
> Thank You
>
>
I don't know why, but my Mean[] seems to be broken?!? So I had to define
it again.
In[1]:=
Unprotect[Mean];
Mean[l_List] := (Plus @@ l)/Length[l];
Protect[Mean];
In[4]:=
data = Table[Random[Real, {-100, 100}], {1036}, {300}, {21}];
In[5]:=
t1 = First@Timing[
data02original = {};
n = Length[data] + 1;
For[i = 1, i < n, i++, step1 = Mean[ColumnTake[data[[i]], {4, 21}]];
AppendTo[data02original, Join[data[[i, 1, {1, 2}]], step1]];]]
Dimensions@data02original
Out[5]=
1.531 Second
Out[6]=
{1036, 20}
In[7]:=
f = Compile[{{d, _Real, 2}},
Join[d[[1, {1, 2}]], ((Plus @@ #1)/Length[#1] &)
[d[[All, Range[4, 21]]]]]];
In[8]:=
First[{t2, data02} = Timing[f /@ data]]
Union@Flatten@Chop[data02 - data02original]
Out[8]=
0.547 Second
Out[9]=
{0}
In[10]:=
t1/t2
Out[10]=
2.7989
--
Peter Pein
Berlin