Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

Re: is there a better way to iterate this?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg78080] Re: is there a better way to iterate this?
  • From: Ray Koopman <koopman at sfu.ca>
  • Date: Fri, 22 Jun 2007 06:45:30 -0400 (EDT)
  • References: <f532ln$3ab$1@smc.vnet.net><f58bpt$72b$1@smc.vnet.net>

On Jun 19, 3:39 am, dantimatter <dantimat... at gmail.com> wrote:
> hi all,
>
> it was pointed out that my original post contained some errors, and
> that it might be more useful if i posted the actual code, so here
> goes:
>
> -----------------------------
>
> moms={{60, 78, 54, 38, 56, 62, 58, 66, 78, 78, 74, 84, 84, z, z, z, z,
> z}, {78,
>    50, 46, 46, 62, 62, 70, 68, 68, 68, 78, 76, 84, 134, z, z, z, z},
> {64, 52,
>     56, 58, 54, 58, 56, 66, 62, 80, 74, 96, 82, z, z, z, z, z}, {122,
> 54,
>    72, 60, 66, 80, 74, 92, 152, z, z, z, z, z, z, z, z, z}, {64,
>  46, 50, 70, 54, 58, 66, 58, 66, 78, 82, 82, 100, z, z, z, z, z}, {58,
> 52,
>    68, 76, 60, 50, 68, 76, 74, 80, 80, 88, 116, z, z, z, z, z}, {72,
> 56, 56,
>    52, 68, 50, 72, 46, 72, 84, 82, 94, 100, z, z, z, z, z}, {76, 58,
> 64, 58,
>    68, 46, 52, 70, 76, 74, 80, 84, 108, z, z, z, z,
>   z}, {64, 60, 68, 40, 62, 48, 68, 62, 74, 72, 72, 94, 90, z, z, z, z,
> z}, {
>    70, 52, 58, 68, 58, 52, 72, 56, 62, 68, 76, 84, 98, z, z,
>    z, z, z}, {66, 74, 74, 76, 54, 54, 64, 82, 128, z, z, z, z, z, z,
> z, z,
>    z}, {54, 62, 64, 72, 76, 74, z, z, z, z, z, z, z, z, z, z, z, z}, {
>    78, 62, 70, 54, 56, 60, 74, z, z, z, z, z, z, z, z, z, z, z}, {66,
> 56, 60,
>     64, 56, 62, 68, 58, 58, 58, 68, 76, 62, 76, 66, 64, 88, 56}, {62,
> 56, 62,
>   44, 50, 48, 48, 66, 48, 48, 60, 50, 52, 58, 66, z, z, z}, {22, 50,
> 58, 60,
>    60, 50, 50, 56, 56, 48, 54, 58, 78, 38, 50, 60, 58, 70}, {62, 56,
> 52,
>    50, 60, 46, 60, 54, 62, 50, 52, 88, 42, 50, 58, 68, 62, z}, {60,
> 48,
>   60, 60, 56, 54, 50, 64, 56, 70, 72, 40, 56, 66, 56, 132, z, z}, {62,
> 56,
>    52, 86, z, z, z, z, z, z, z, z, z, z, z, z, z, z}, {64, 54, 64, 56,
> 76,
>  z, z, z, z, z, z, z, z, z, z, z, z, z}, {58, 52, 60, 48, 54, 56, 50,
> 60,
>    56, 54, 54, 62, 50, 44, 62, 56, 56, z}};
>
> daughters={{z, z, z, z, z, z, 74, 74, 56, 66, 84, 108, 124, z, z, z,
> z, z}, {z, z, 50,
>    50, 66, 64, 66, 72, 76, 90, 70, 90, 120, z, z, z, z, z}, {z, z, z,
> z,
>     z, 60, 70, 78, 72, 80, 106, 108, z, z, z, z, z, z}, {z, 70, 70,
> 68, 78,
>     88, 116, z, z, z, z, z, z, z, z, z, z, z}, {z, z,
>   z, z, z, z, z, z, z, 84, 84, 94, 112, z, z, z, z, z}, {z, z, z, z,
> z,
>    62, 62, 62, 74, 68, 106, 94, 100, z, z, z, z, z}, {z, z, z,
>   z, z, z, z, z, 64, 68, 90, 94, 116, z, z, z, z, z}, {z, z, z, z, 60,
> 56,
>    74, 68, 90, 106, z, z, z, z, z, z, z, z}, {z, z, z,
>  z, z, z, 62, 60, 60, 70, 88, 94, 86, z, z, z, z, z}, {z, z, z, z, z,
> z,
>    44, 68, 56, 64, 84, 82, 92, 92, z, z, z, z}, {z,
>   z, z, z, z, 62, 56, 102, 56, z, z, z, z, z, z, z, z, z}, {z, z, z,
> z, z,
>    z, z, z, z, z, z, z, z, z, z, z, z, z}, {z, z, 62, 68, 60,
>   66, 82, 48, 74, z, z, z, z, z, z, z, z, z}, {z, z, z, z, z, z, z, z,
> z, z,
>    z, z, z, 140, 74, z, z, z}, {z, z, z, z, z, 40, 52, 50, 54, 64, 64,
>    62, 54, 62, 74, 64, 62, z}, {z, 84, 80, 62, 60, 50, 54, 68, 70, 60,
>    64, 62, 78, 66, 60, z, z, z}, {z, z, z, z, z, z, 52, 64, 58, 56,
> 64,
>    62, 62, 64, 62, 60, 64, z}, {z, z, z, z, z, z, z, 54, 56, 60, 90,
> 42, 60,
>     z, z, z, z, z}, {z, z, 54, 52, 58, 60, 56, 60, 60, 56, 58, 64, 64,
>    58, 64, z, z, z}, {z, 64, 56, 52, 56, 52, 66, 64, 62, z, z, z, z,
> z, z,
>  z, z, z}, {z, z, z, z, 56, 56, 68, 68, 50, 56, 58, 66, 72, 58, 54,
> 78, z, z}
>    };
>
> randomdaughters = Table[RandomPermutation[daughters], {10000}];
>
> randommomdaughterpairs = Table[Table[Transpose[{moms[[j]],
> randomdaughters[[k, j]]}], {j, Length[moms]}], {k,
> Length[randomdaughters]}];
>
> gens=Table[
>      Table[randommomdaughterpairs[[k,All,i]],{i,
>          Length[randommomdaughterpairs[[k,1]]]}],{k,
>        Length[randommomdaughterpairs]}];
>
> mddivtimepergen=
>    Table[
>      Table[DeleteCases[gens[[j,k]],_?(#[[1]]==z||#[[2]]==z&)],{k,
>          Length[gens[[j]]]}],{j,Length[gens]}];
>
> coeffs=Table[
>      N[Correlation[Flatten[mddivtimepergen[[i]],1][[All,1]],
>          Flatten[mddivtimepergen[[i]],1][[All,2]]]],{i,
>        Length[mddivtimepergen]}];
>
> ---------------------------
>
> any suggestions on how to make this more efficient would be much
> appreciated.
> thanks to all,
> dan

This does the correlations one at a time. It's probably not as fast
as if it were vectorized, but I think it's easier to understand.

In[1]:= <<DiscreteMath`Combinatorica`
        <<Statistics`MultiDescriptiveStatistics`

In[3]:= Dimensions[moms = { << ... >> }]
Out[3]= {21,18}

In[4]:= Dimensions[daughters = { << ... >> }]
Out[4]= {21,18}

In[5]:= corr[m_,d_] := Correlation @@ Transpose @ DeleteCases[
             Flatten[Transpose[{m,d},{3,1,2}],1], {z,_}|{_,z}]

In[6]:= nmoms = N[moms]; ndaughters = N[daughters];
        N@corr[moms,daughters]
        corr[nmoms,ndaughters]
Out[7]= 0.589018
Out[8]= 0.589018

In[9]:= Timing[r = Table[corr[nmoms,
               RandomPermutation@ndaughters],{n = 1*^4}];][[1]]
        {n, m = Mean@r, StandardDeviation@r}
        ListPlot[Transpose@{Sort@r,Range[.5,n]/n}, Frame->True,
                 PlotJoined->True, AxesOrigin->{m,.5}]
Out[9]= 20.68 Second
Out[10]= {10000,0.173864,0.113114}
        -Graphics-



  • Prev by Date: Re: 6.0 Standard Packages?, New Style Documentation?
  • Next by Date: Re: My problem when solving a system of equations
  • Previous by thread: Re: is there a better way to iterate this?
  • Next by thread: more gripe about the new documentation center (DC) in Mathematica 6