4
$\begingroup$

I have created this setup, it is giving me the correct answer, however I am interested in a function that does all this calculations and creates a output of mean and standard deviation. The function is has to be able to set the limit which i have named y, and run over a list.

In[302]:= list = RandomReal[{-10, 10}, 20]

Out[302]= {4.48825, 3.31096, -5.1268, -2.11263, 2.04758, 7.41939, \
1.37117, 4.73285, -1.38028, 4.55688, 6.2333, 5.67909, 6.41423, \
0.709616, -3.75265, -3.16793, 2.10447, 0.583272, -0.370173, -8.65539}

In[303]:= pp = Range[Length[list]];

In[304]:= y = 2;

In[305]:= vt = If[(-y > list[[#]] || list[[#]] > y), yes, no] & /@ pp

Out[305]= {yes, yes, yes, yes, yes, yes, no, yes, no, yes, yes, yes, \
yes, no, yes, yes, yes, no, no, yes}

In[306]:= groups = SplitBy[vt, # == yes]

Out[306]= {{yes, yes, yes, yes, yes, yes}, {no}, {yes}, {no}, {yes, 
yes, yes, yes}, {no}, {yes, yes, yes}, {no, no}, {yes}}

In[307]:= fpt = DeleteCases[groups, no, {2}]

Out[307]= {{yes, yes, yes, yes, yes, yes}, {}, {yes}, {}, {yes, yes, 
yes, yes}, {}, {yes, yes, yes}, {}, {yes}}

In[308]:= rp = Range[Length[fpt]];

In[309]:= bob = Length[fpt[[#]]] & /@ rp;

In[310]:= countLength = DeleteCases[bob, 0];

In[311]:= N[StandardDeviation[countLength]]

Out[311]= 2.12132

In[312]:= N[Mean[countLength]]

Out[312]= 3.` 
$\endgroup$
3
  • 1
    $\begingroup$ This is not a free coding service. You should try to build the function yourself. If you run into difficulties, then ask about the specific problem you have run into, $\endgroup$
    – m_goldberg
    Sep 17, 2013 at 12:44
  • $\begingroup$ I understand that, I have spent all day trying to come up with a solution I have pages on pages with different ideas, but no one looks even close to be able to solve the problem. I also thought that this was a pretty simple coding for most of the people on this forum, so I guessed it would be easier to let you guys show me how to do it then to play around with my bad coding. $\endgroup$
    – ALEXANDER
    Sep 17, 2013 at 12:53
  • 3
    $\begingroup$ @m_goldberg, give the man a break. OP obviously tried some. $\endgroup$
    – Hector
    Sep 17, 2013 at 12:55

3 Answers 3

6
$\begingroup$
g[l_List, y_] := N@{Mean@#, StandardDeviation@#} &@(Length /@ 
                                     Cases[Split[Not[-y < # < y ] & /@ l], {True..}])
$\endgroup$
7
  • $\begingroup$ Hi! This looks amazing, would you be able to explain what is going on in the code? $\endgroup$
    – ALEXANDER
    Sep 17, 2013 at 12:55
  • 1
    $\begingroup$ @ALEXANDER I'm sure you can figure it out yourself. Just start trying the expressions inside out. Try -y < # < y & /@ l first, then add Not[], then Split[], and so on $\endgroup$ Sep 17, 2013 at 12:58
  • $\begingroup$ Okey, thanks, also one more question, how long have you been programming? $\endgroup$
    – ALEXANDER
    Sep 17, 2013 at 13:00
  • $\begingroup$ @ALEXANDER For longer than I want to confess $\endgroup$ Sep 17, 2013 at 13:05
  • 1
    $\begingroup$ @ALEXANDER Your code is not bad, that was the reason because I decided to answer and disregard goldberg's comment above. Perhaps a good book can help you to progress faster mathematica.stackexchange.com/questions/18/… $\endgroup$ Sep 17, 2013 at 13:17
3
$\begingroup$

Gather your code as follows:

Module[{pp = Range[Length[list]], y = 2, vt, groups, fpt, rp, bob, countLength}, 
 vt = If[(-y > list[[#]] || list[[#]] > y), yes, no] & /@ pp; 
 groups = Split[vt]; fpt = DeleteCases[groups, no, {2}]; 
 rp = Range[Length[fpt]]; bob = Length[fpt[[#]]] & /@ rp; 
 countLength = DeleteCases[bob, 0];
 {N[StandardDeviation[countLength]], N[Mean[countLength]]}]

and check that it runs. The syntax is basically

Module[{vars}, code;eachLineSeparated;bySemicolons;Result]

Once you check it works, define the function

theFunction[y_][list_] := Module[{pp = Range[Length[list]], vt, groups, fpt, rp, bob, 
 countLength}, vt = If[(-y > list[[#]] || list[[#]] > y), yes, no] & /@ pp; 
  groups = Split[vt]; fpt = DeleteCases[groups, no, {2}]; 
  rp = Range[Length[fpt]]; bob = Length[fpt[[#]]] & /@ rp; 
  countLength = DeleteCases[bob, 0];
  {N[StandardDeviation[countLength]], N[Mean[countLength]]}]
$\endgroup$
3
  • $\begingroup$ Thank you Hector, smart way to gather and check the code, what I have done is extremely bad, but I have to start somewhere. I constantly experience new things that I am wondering about is there any easy way to example figure out what [y_][list_] is doing. $\endgroup$
    – ALEXANDER
    Sep 17, 2013 at 13:19
  • $\begingroup$ Quick and dirty, get the job done. 15 years later, you can produce the sleek code as shown in other posts. Then, you will begin thinking about optimizing the code, etc. Keep up the interest. As for f[x_][y_], because of the := that follows, it defines a function. Almost like f[x_,y_], except that f[x] is itself a function. You might want check reference.wolfram.com/mathematica/ref/…. $\endgroup$
    – Hector
    Sep 17, 2013 at 13:23
  • $\begingroup$ Thank you, I will definitely keep up the interest. Mathematica is an amazing tool. $\endgroup$
    – ALEXANDER
    Sep 17, 2013 at 13:40
1
$\begingroup$

I believe this code produces the desired result:

ms[data_, 
  y_] := {Mean[#], N@StandardDeviation[#]} &@(Length /@ 
    Select[SplitBy[Boole[Abs[#] > y] & /@ data, 1], MemberQ[#, 1] &])

Using:

data = {4.48825, 3.31096, -5.1268, -2.11263, 2.04758, 7.41939, 
  1.37117, 4.73285, -1.38028, 4.55688, 6.2333, 5.67909, 6.41423, 
  0.709616, -3.75265, -3.16793, 2.10447, 0.583272, -0.370173, -8.65539}

ms[data,2] yields:

{3, 2.12132}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.