Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a few natural ways to do it.</p> <p>You could form the list of arguments to your "percentage decrease" function using <a href="http://reference.wolfram.com/mathematica/ref/Partition.html" rel="nofollow noreferrer">Partition</a>:</p> <pre><code>In[3]:= list = {a, b, c, d, e}; In[4]:= Partition[list, 2, 1] Out[4]= {{a, b}, {b, c}, {c, d}, {d, e}} </code></pre> <p>Then you can <a href="http://reference.wolfram.com/mathematica/ref/Apply.html" rel="nofollow noreferrer">Apply</a> a function to these:</p> <pre><code>In[6]:= f @@@ Partition[list, 2, 1] Out[6]= {f[a, b], f[b, c], f[c, d], f[d, e]} </code></pre> <p>Using your percent decrease function:</p> <pre><code>In[7]:= PercentDecrease[a_, b_] := (b - a)/a In[8]:= PercentDecrease @@@ Partition[list, 2, 1] Out[8]= {(-a + b)/a, (-b + c)/b, (-c + d)/c, (-d + e)/d} </code></pre> <p>(Read about @@@ by looking at the "More Information" notes at <a href="http://reference.wolfram.com/mathematica/ref/Apply.html" rel="nofollow noreferrer">Apply</a>.)</p> <p>Instead of Partition you can use <a href="http://reference.wolfram.com/mathematica/ref/Most.html" rel="nofollow noreferrer">Most</a> and <a href="http://reference.wolfram.com/mathematica/ref/Rest.html" rel="nofollow noreferrer">Rest</a> to form lists of the first and second arguments and then combine them using <a href="http://reference.wolfram.com/mathematica/ref/MapThread.html" rel="nofollow noreferrer">MapThread</a>:</p> <pre><code>In[14]:= MapThread[PercentDecrease, {Most[list], Rest[list]}] Out[14]= {(-a + b)/a, (-b + c)/b, (-c + d)/c, (-d + e)/d} </code></pre> <p>A different way is to form your operation (a subtraction and a division) in two steps like this:</p> <pre><code>In[10]:= Differences[list] / Most[list] Out[10]= {(-a + b)/a, (-b + c)/b, (-c + d)/c, (-d + e)/d} </code></pre> <p>The divide operation (/) <a href="http://reference.wolfram.com/mathematica/ref/Listable.html" rel="nofollow noreferrer">threads</a> over the two lists <code>Differences[list]</code> and <code>Most[list</code>].</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload