Difference between revisions of "Array-reducing functions"
Line 4: | Line 4: | ||
__TOC__ | __TOC__ | ||
− | An array-reducing function operates across a dimension of an array and returns a result that has one dimension less than the number of dimensions of its input array. When applied to an array of <code>n</code> dimensions, a reducing function produces an array that contains <code>n-1</code> dimensions. Examples include, | + | An array-reducing function operates across a dimension of an array and returns a result that has one dimension less than the number of dimensions of its input array. When applied to an array of <code>n</code> dimensions, a reducing function produces an array that contains <code>n-1</code> dimensions. Examples include, [[Sum]](x, i), [[Product]](x,i), [[Max]](x, i), [[Min]](x, i), and others described below. The subscript construct [[Subscript-Slice_Operator|x[i = v]]] and related subscript and slice functions also reduce arrays by a dimension (see [[Subscript and slice of a subarray]]). |
− | The function | + | The function [[Sum]](x, i) illustrates some properties of reducing functions. |
'''Examples''' | '''Examples''' | ||
− | <code>Sum(Car_prices, Car_type) →</code> | + | :<code>Sum(Car_prices, Car_type) →</code> |
− | + | :{| class="wikitable" border="1" | |
− | {| class="wikitable" border="1" | ||
! colspan="5" | Years ▶ | ! colspan="5" | Years ▶ | ||
|- | |- | ||
Line 28: | Line 27: | ||
|} | |} | ||
− | <code>Sum(Car_prices, Years) →</code> | + | :<code>Sum(Car_prices, Years) →</code> |
− | + | :{| class="wikitable" border="1" | |
− | {| class="wikitable" border="1" | ||
! colspan="3" | Car_type ▶ | ! colspan="3" | Car_type ▶ | ||
|- | |- | ||
Line 42: | Line 40: | ||
|} | |} | ||
− | <code>Sum(Sum(Car_prices, Years), Car_type) → 334K</code> | + | :<code>Sum(Sum(Car_prices, Years), Car_type) → 334K</code> |
''See [[Array Function Example Variables]] for example array variables used here and below.'' | ''See [[Array Function Example Variables]] for example array variables used here and below.'' | ||
− | <tip title="Tip">The second parameter, | + | <tip title="Tip">The second parameter, «i», specifying the dimension over which to sum, is optional. But if the array, «x», has more than one dimension, Analytica might not sum over the dimension you expect. For this reason, it is safer always to specify the dimension index explicitly in [[Sum]]() or any other array-reducing function.</Tip> |
+ | |||
+ | '''Reducing over an unused index:''' If the index, «i», is not a dimension of «x», [[Sum]](x, i) returns «x» unreduced (i.e., with the same number of indexes), but multiplied by the size (number of elements) of «i». The reason is that if «x» is not indexed by «i», it means that it has the same value for all values of «i». This is true even if «x» is an atom with no dimensions: | ||
− | + | :<code>Variable x := 5</code> | |
− | + | :<code>Sum(x, Car_type) → 15</code> | |
− | |||
This is because <code>Car_type</code> has three elements (3x5 = 15). For <code>Product</code>: | This is because <code>Car_type</code> has three elements (3x5 = 15). For <code>Product</code>: | ||
− | + | :<code>Product(x, Car_type) → 125</code> | |
That is, it multiplies <code>x</code> three times (5<sup>3</sup> = 125). | That is, it multiplies <code>x</code> three times (5<sup>3</sup> = 125). | ||
− | In this way, if we later decide to change the value for x for each value of <code>Car_type</code>, we can redefine | + | In this way, if we later decide to change the value for <code>x</code> for each value of <code>Car_type</code>, we can redefine «x» as an edit table indexed by <code>Car_type</code>. Any expression containing a '''[[Sum]]()''' or other reducing function on «x» works correctly whether it is indexed by <code>Car_type</code> or not. |
− | '''Elements that are ignored:''' The array-reducing functions described in this section ignore elements of an array that have the special value | + | '''Elements that are ignored:''' The array-reducing functions described in this section ignore elements of an array that have the special value [[Null]]. For example, the [[Average]](x, i) function sums all the non-null elements of «x» and divide by the number of elements that are not [[Null]]. |
− | When a '''NaN''' value (signifying an indeterminate number) appears as an element of an array, the result of the function that operates on the array will usually be '''NaN''' as well. '''NaN''' values result from indeterminate operations such as <code>0/0</code>, and the fact that they propagate forward in this fashion helps ensure that you will not accidentally compute an indeterminate result without realizing it. However, in some cases you might wish to ignore '''NaN''' values in an array-reducing operation. The array-reducing functions | + | When a '''NaN''' value (signifying an indeterminate number) appears as an element of an array, the result of the function that operates on the array will usually be '''NaN''' as well. '''NaN''' values result from indeterminate operations such as <code>0/0</code>, and the fact that they propagate forward in this fashion helps ensure that you will not accidentally compute an indeterminate result without realizing it. However, in some cases you might wish to ignore '''NaN''' values in an array-reducing operation. The array-reducing functions [[Sum]], [[Product]], [[Average]], [[Min]], and [[Max]] all accept an optional parameter, «ignoreNaN» that can be set to True. «IgnoreNan» requires a named-parameter syntax, for example: |
− | |||
− | When you operate over an array containing some text and some numeric values, the Sum, Min and Max functions can be instructed to ignore all the non-numeric values using an optional ''' | + | :<code>Max(x, i, ignoreNaN: True)</code> |
− | + | ||
+ | When you operate over an array containing some text and some numeric values, the [[Sum]], [[Min]] and [[Max]] functions can be instructed to ignore all the non-numeric values using an optional «ignoreNonNumbers» parameter, for example: | ||
+ | |||
+ | :<code>Max(x, i, ignoreNonNumbers: True)</code> | ||
+ | |||
+ | '''Reducing over multiple indexes:''' The array-reducing functions [[Sum]], [[Product]], [[Average]], [[Min]], [[Max]], [[ArgMin]], and [[ArgMax]] all allow you to specify more than one index as a convenient way to reduce over multiple indexes in a single call. For example: | ||
+ | |||
+ | :<code>Sum(x, i, j, k)</code> | ||
− | |||
− | |||
− | |||
This is equivalent to: | This is equivalent to: | ||
− | + | :<code>Sum(Sum(Sum(x, i), j), k)</code> | |
==Sum(x, ''i'')== | ==Sum(x, ''i'')== | ||
− | Returns the sum of array | + | Returns the sum of array «x» over the dimension indexed by «i». |
'''Library:''' Array | '''Library:''' Array | ||
Line 79: | Line 81: | ||
'''Examples:''' | '''Examples:''' | ||
− | <code>Sum(Car_prices, Years) →</code> | + | :<code>Sum(Car_prices, Years) →</code> |
− | {| class="wikitable" border="1" | + | :{| class="wikitable" border="1" |
! colspan="3" | Car_type ▶ | ! colspan="3" | Car_type ▶ | ||
|- | |- | ||
Line 96: | Line 98: | ||
==Product(x,''i'')== | ==Product(x,''i'')== | ||
− | Returns the product of all of the elements of | + | Returns the product of all of the elements of «x», along the dimension indexed by «i». See also [[Product]](). |
'''Library:''' Array | '''Library:''' Array | ||
Line 102: | Line 104: | ||
'''Examples:''' | '''Examples:''' | ||
− | <code>Product(Car_prices, Car_type) →</code> | + | :<code>Product(Car_prices, Car_type) →</code> |
− | {| class="wikitable" border="1" | + | :{| class="wikitable" border="1" |
! colspan="5" style="text-align: left;" | Years ▶ | ! colspan="5" style="text-align: left;" | Years ▶ | ||
|- | |- | ||
Line 120: | Line 122: | ||
==Average(x, ''i'')== | ==Average(x, ''i'')== | ||
− | Returns the mean value of all of the elements of array | + | Returns the mean value of all of the elements of array «x», averaged over index «i». See also [[Average]](). |
'''Library:''' Array | '''Library:''' Array | ||
Line 126: | Line 128: | ||
'''Examples:''' | '''Examples:''' | ||
− | <code>Average(Miles, Years)→</code> | + | :<code>Average(Miles, Years)→</code> |
− | + | :{| class="wikitable" border="1" | |
− | {| class="wikitable" border="1" | ||
! colspan="3" | Car_type ▶ | ! colspan="3" | Car_type ▶ | ||
|- | |- | ||
Line 141: | Line 142: | ||
==Max(x, i)== | ==Max(x, i)== | ||
− | Returns the highest valued element of | + | Returns the highest valued element of «x» along index «i». See also [[Max]](). |
'''Library:''' Array | '''Library:''' Array | ||
Line 147: | Line 148: | ||
'''Examples:''' | '''Examples:''' | ||
− | <code>Max(Miles, Years) →</code> | + | :<code>Max(Miles, Years) →</code> |
− | + | :{| class="wikitable" border="1" | |
− | {| class="wikitable" border="1" | ||
! colspan="3" | Car_type ▶ | ! colspan="3" | Car_type ▶ | ||
|- | |- | ||
Line 162: | Line 162: | ||
To obtain the maximum of two numbers, first turn them into an array: | To obtain the maximum of two numbers, first turn them into an array: | ||
− | + | ||
+ | :<code>Max([10, 5]) → 10</code> | ||
''See [[Array Function Example Variables]] for example array variables used here and below.'' | ''See [[Array Function Example Variables]] for example array variables used here and below.'' | ||
==Min(x, ''i'')== | ==Min(x, ''i'')== | ||
− | Returns the lowest valued element of | + | Returns the lowest valued element of «x» along index «i». See also [[Min]](). |
'''Library:''' Array | '''Library:''' Array | ||
Line 173: | Line 174: | ||
'''Examples:''' | '''Examples:''' | ||
− | <code>Min(Miles, Years) →</code> | + | :<code>Min(Miles, Years) →</code> |
− | + | :{| class="wikitable" border="1" | |
− | {| class="wikitable" border="1" | ||
! colspan="3" | Car_type ▶ | ! colspan="3" | Car_type ▶ | ||
|- | |- | ||
Line 189: | Line 189: | ||
To obtain the minimum of two numbers, first turn them into an array: | To obtain the minimum of two numbers, first turn them into an array: | ||
− | + | :<code>Min([10, 5]) → 5</code> | |
==ArgMax(a, i)== | ==ArgMax(a, i)== | ||
− | Returns the item of index | + | Returns the item of index «i» for which array «a» is the maximum. If «a» has more than one value equal to the maximum, it returns the index of the last one. See also [[ArgMax]](). |
'''Library:''' Array | '''Library:''' Array | ||
'''Example:''' | '''Example:''' | ||
− | + | :<code>ArgMax(Miles, Car_type) →</code> | |
− | <code>ArgMax(Miles, Car_type) →</code> | + | :{| class="wikitable" border="1" |
− | |||
− | {| class="wikitable" border="1" | ||
! colspan="5" style="text-align: left;" | Years ▶ | ! colspan="5" style="text-align: left;" | Years ▶ | ||
|- | |- | ||
Line 217: | Line 215: | ||
==ArgMin(a, i)== | ==ArgMin(a, i)== | ||
− | Returns the corresponding value in index | + | Returns the corresponding value in index «i» for which array «a» is the minimum. If more than one value equals the minimum, returns the index of the last occurrence. See also [[ArgMin]](). |
'''Library:''' Array | '''Library:''' Array | ||
'''Example:''' | '''Example:''' | ||
− | + | :<code>ArgMin(Car_prices, Car_type) →</code> | |
− | <code>ArgMin(Car_prices, Car_type) →</code> | + | :{| class="wikitable" border="1" |
− | |||
− | {| class="wikitable" border="1" | ||
! colspan="5" style="text-align: left;" | Years ▶ | ! colspan="5" style="text-align: left;" | Years ▶ | ||
|- | |- | ||
Line 242: | Line 238: | ||
==CondMin(x, cond, i), CondMax(x, cond, i)== | ==CondMin(x, cond, i), CondMax(x, cond, i)== | ||
− | Conditional Min and Max. | + | Conditional Min and Max. [[CondMin]]() returns the smallest, and [[CondMax]]() returns the largest values along a given index, «i», that satisfies condition cond. When «cond» is never satisfied, [[CondMin]]() returns <code>INF</code>, [[CondMax]]() returns <code>-INF</code>. |
− | values along a given index, | ||
'''Library:''' none | '''Library:''' none | ||
'''Examples:''' | '''Examples:''' | ||
− | + | :<code>CondMin(Cost_of_ownership, Time >= 2, Time) →</code> | |
− | <code>CondMin(Cost_of_ownership, Time >= 2, Time) →</code> | + | :{| class="wikitable" border="1" |
− | |||
− | {| class="wikitable" border="1" | ||
! colspan="3" | Car_type ▶ | ! colspan="3" | Car_type ▶ | ||
|- | |- | ||
Line 263: | Line 256: | ||
|} | |} | ||
− | == | + | ==SubIndex(a, u, i)== |
− | Returns the value of index | + | Returns the value of index «i» for which array «a» (indexed by «i») is equal to «u». If more than one value of a equals «u», it returns the last value of «i» that matches «u». If no value of «a» equals «u», it returns [[Null]]. If «a» has index(es) in addition to «i», or if «u» is an array with other indexes, those indexes also appear in the result. See also [[SubIndex]](). |
'''Library:''' Array | '''Library:''' Array | ||
'''Examples:''' | '''Examples:''' | ||
− | + | :<code>SubIndex(Car_prices, 18K, Car_type) →</code> | |
− | <code> | + | :{| class="wikitable" border="1" |
− | |||
− | {| class="wikitable" border="1" | ||
! colspan="5" style="text-align: left;" | Years ▶ | ! colspan="5" style="text-align: left;" | Years ▶ | ||
|- | |- | ||
Line 288: | Line 279: | ||
|} | |} | ||
− | <code> | + | :<code>SubIndex(Car_prices, 18K, Years) →</code> |
− | + | :{| class="wikitable" border="1" | |
− | {| class="wikitable" border="1" | ||
! colspan="3" | Car_type ▶ | ! colspan="3" | Car_type ▶ | ||
|- | |- | ||
Line 302: | Line 292: | ||
|} | |} | ||
− | If | + | If «u» is an array of values, an array of index values is returned. |
− | <code>SubIndex(Car_prices, [18K, 19K] Car_type) →</code> | + | :<code>SubIndex(Car_prices, [18K, 19K] Car_type) →</code> |
− | {| class="wikitable" | + | :{| class="wikitable" |
! | ! | ||
! colspan="5" |Years ▶ | ! colspan="5" |Years ▶ | ||
Line 332: | Line 322: | ||
==PositionInIndex(a, x, i)== | ==PositionInIndex(a, x, i)== | ||
− | Returns the position in index | + | Returns the position in index «i» — that is, a number from 1 to the size of index «i» — of the last element of array «a» equal to «x»; if no element is equal, it returns 0. See also [[PositionInIndex]](). |
− | When array | + | When array «a» is multidimensional, the result is reduced by one dimension, dimension «i». |
'''Library:''' Array | '''Library:''' Array | ||
Line 341: | Line 331: | ||
When the array is one-dimensional: | When the array is one-dimensional: | ||
− | + | :<code>Index I := ['A', 'B', 'C']</code> | |
− | + | :<code>Variable A := Array(I, [1, 2, 2])</code> | |
− | + | :<code>PositionInIndex(A, 1, I) → 1</code> | |
− | + | :<code>PositionInIndex(A, 2, I) → 3</code> | |
− | + | :<code>PositionInIndex(A, 5, I) → 0</code> | |
− | <tip title="Tip"> | + | <tip title="Tip">[[PositionInIndex]]() is the positional equivalent of [[Subindex]](). It is useful when «i» contains duplicate values, in which case [[Subindex]]() would return an ambiguous result.</Tip> |
<tip title="Tip">Parameter a is optional. When omitted, it returns the position of x in the index i, or 0 if not found. The syntax <code>@[i = x]</code> (see [[Subscript and slice of a subarray#@: Index Position Operator|@: Index Position Operator]]) returns the same result as <code>PositionInIndex(, x, i)</code>: | <tip title="Tip">Parameter a is optional. When omitted, it returns the position of x in the index i, or 0 if not found. The syntax <code>@[i = x]</code> (see [[Subscript and slice of a subarray#@: Index Position Operator|@: Index Position Operator]]) returns the same result as <code>PositionInIndex(, x, i)</code>: | ||
− | + | ||
− | + | :<code>PositionInIndex(, 'B', I) → 2</code> | |
− | + | :<code>@[I = 'B'] → 2</code> | |
− | + | :<code>PositionInIndex(, 'D', I) → 0</code> | |
+ | :<code>@[I = 'D'] → 0</code> | ||
</Tip> | </Tip> | ||
Line 359: | Line 350: | ||
Taking the same example from above: | Taking the same example from above: | ||
− | <code>PositionInIndex(Car_prices, 18K, Car_type) →</code> | + | :<code>PositionInIndex(Car_prices, 18K, Car_type) →</code> |
− | + | :{| class="wikitable" border="1" | |
− | {| class="wikitable" border="1" | ||
! colspan="5" style="text-align: left;" | Years ▶ | ! colspan="5" style="text-align: left;" | Years ▶ | ||
|- | |- | ||
Line 379: | Line 369: | ||
==Area(y, x, ''x1, x2, i'')== | ==Area(y, x, ''x1, x2, i'')== | ||
Returns the area (sum of trapezoids) under the piecewise-linear curve denoted by the points | Returns the area (sum of trapezoids) under the piecewise-linear curve denoted by the points | ||
− | + | («x<sub>i</sub>», «y<sub>i</sub>»), landing in the region «x1» ≤ «x» ≤ «x2». The arrays «x» and «y» must share the common index «i», or when either «x» or «y» is itself an index, «i» can be safely omitted. «x» and «x2» are optional; if they are not specified, the area is calculated across all values of «x». | |
− | If | + | If «x1» or «x2» fall outside the range of values in «i», the first value (for «x1») or last value (for «x1») are used. [[Area]]() computes the total integral across «x», returning a value with one less dimension than «y». Compare [[Area]]() to [[Transforming functions#Integrate()|Integrate()]]. |
'''Library:''' Array | '''Library:''' Array | ||
'''Example''' | '''Example''' | ||
− | + | :<code>Area(Cost_of_ownership, Time, 0, 2) →</code> | |
− | <code>Area(Cost_of_ownership, Time, 0, 2) →</code> | + | :{| class="wikitable" border="1" |
− | {| class="wikitable" border="1" | ||
! colspan="3" | Car_type ▶ | ! colspan="3" | Car_type ▶ | ||
|- | |- | ||
Line 414: | Line 403: | ||
* [[PositionInIndex]]() | * [[PositionInIndex]]() | ||
* [[Area]]() | * [[Area]]() | ||
+ | |||
+ | |||
<footer>Functions that create arrays / {{PAGENAME}} / Transforming functions</footer> | <footer>Functions that create arrays / {{PAGENAME}} / Transforming functions</footer> |
Revision as of 04:40, 29 December 2015
An array-reducing function operates across a dimension of an array and returns a result that has one dimension less than the number of dimensions of its input array. When applied to an array of n
dimensions, a reducing function produces an array that contains n-1
dimensions. Examples include, Sum(x, i), Product(x,i), Max(x, i), Min(x, i), and others described below. The subscript construct x[i = v] and related subscript and slice functions also reduce arrays by a dimension (see Subscript and slice of a subarray).
The function Sum(x, i) illustrates some properties of reducing functions.
Examples
Sum(Car_prices, Car_type) →
Years ▶ 2005 2006 2007 2008 2009 59K 62K 66K 71K 76K
Sum(Car_prices, Years) →
Car_type ▶ VW Honda BMW 99K 103K 141K
Sum(Sum(Car_prices, Years), Car_type) → 334K
See Array Function Example Variables for example array variables used here and below.
Reducing over an unused index: If the index, «i», is not a dimension of «x», Sum(x, i) returns «x» unreduced (i.e., with the same number of indexes), but multiplied by the size (number of elements) of «i». The reason is that if «x» is not indexed by «i», it means that it has the same value for all values of «i». This is true even if «x» is an atom with no dimensions:
Variable x := 5
Sum(x, Car_type) → 15
This is because Car_type
has three elements (3x5 = 15). For Product
:
Product(x, Car_type) → 125
That is, it multiplies x
three times (53 = 125).
In this way, if we later decide to change the value for x
for each value of Car_type
, we can redefine «x» as an edit table indexed by Car_type
. Any expression containing a Sum() or other reducing function on «x» works correctly whether it is indexed by Car_type
or not.
Elements that are ignored: The array-reducing functions described in this section ignore elements of an array that have the special value Null. For example, the Average(x, i) function sums all the non-null elements of «x» and divide by the number of elements that are not Null.
When a NaN value (signifying an indeterminate number) appears as an element of an array, the result of the function that operates on the array will usually be NaN as well. NaN values result from indeterminate operations such as 0/0
, and the fact that they propagate forward in this fashion helps ensure that you will not accidentally compute an indeterminate result without realizing it. However, in some cases you might wish to ignore NaN values in an array-reducing operation. The array-reducing functions Sum, Product, Average, Min, and Max all accept an optional parameter, «ignoreNaN» that can be set to True. «IgnoreNan» requires a named-parameter syntax, for example:
Max(x, i, ignoreNaN: True)
When you operate over an array containing some text and some numeric values, the Sum, Min and Max functions can be instructed to ignore all the non-numeric values using an optional «ignoreNonNumbers» parameter, for example:
Max(x, i, ignoreNonNumbers: True)
Reducing over multiple indexes: The array-reducing functions Sum, Product, Average, Min, Max, ArgMin, and ArgMax all allow you to specify more than one index as a convenient way to reduce over multiple indexes in a single call. For example:
Sum(x, i, j, k)
This is equivalent to:
Sum(Sum(Sum(x, i), j), k)
Sum(x, i)
Returns the sum of array «x» over the dimension indexed by «i».
Library: Array
Examples:
Sum(Car_prices, Years) →
Car_type ▶ VW Honda BMW 99K 103K 141K
See Array Function Example Variables for example array variables used here and below.
Product(x,i)
Returns the product of all of the elements of «x», along the dimension indexed by «i». See also Product().
Library: Array
Examples:
Product(Car_prices, Car_type) →
Years ▶ 2005 2006 2007 2008 2009 7.2T 8.398T 10.08Y 12.54T 15.36T
Average(x, i)
Returns the mean value of all of the elements of array «x», averaged over index «i». See also Average().
Library: Array
Examples:
Average(Miles, Years)→
Car_type ▶ VW Honda BMW 8000 12K 7600
Max(x, i)
Returns the highest valued element of «x» along index «i». See also Max().
Library: Array
Examples:
Max(Miles, Years) →
Car_type ▶ VW Honda BMW 10K 12K 10K
To obtain the maximum of two numbers, first turn them into an array:
Max([10, 5]) → 10
See Array Function Example Variables for example array variables used here and below.
Min(x, i)
Returns the lowest valued element of «x» along index «i». See also Min().
Library: Array
Examples:
Min(Miles, Years) →
Car_type ▶ VW Honda BMW 6000 10K 5000
To obtain the minimum of two numbers, first turn them into an array:
Min([10, 5]) → 5
ArgMax(a, i)
Returns the item of index «i» for which array «a» is the maximum. If «a» has more than one value equal to the maximum, it returns the index of the last one. See also ArgMax().
Library: Array
Example:
ArgMax(Miles, Car_type) →
Years ▶ 2005 2006 2007 2008 2009 Honda Honda Honda Honda Honda
ArgMin(a, i)
Returns the corresponding value in index «i» for which array «a» is the minimum. If more than one value equals the minimum, returns the index of the last occurrence. See also ArgMin().
Library: Array
Example:
ArgMin(Car_prices, Car_type) →
Years ▶ 2005 2006 2007 2008 2009 BMW VW BMW VW VW
CondMin(x, cond, i), CondMax(x, cond, i)
Conditional Min and Max. CondMin() returns the smallest, and CondMax() returns the largest values along a given index, «i», that satisfies condition cond. When «cond» is never satisfied, CondMin() returns INF
, CondMax() returns -INF
.
Library: none
Examples:
CondMin(Cost_of_ownership, Time >= 2, Time) →
Car_type ▶ VW Honda BMW 3098 3897 3409
SubIndex(a, u, i)
Returns the value of index «i» for which array «a» (indexed by «i») is equal to «u». If more than one value of a equals «u», it returns the last value of «i» that matches «u». If no value of «a» equals «u», it returns Null. If «a» has index(es) in addition to «i», or if «u» is an array with other indexes, those indexes also appear in the result. See also SubIndex().
Library: Array
Examples:
SubIndex(Car_prices, 18K, Car_type) →
Years ▶ 2005 2006 2007 2008 2009 Honda «null» Honda «null» «null»
SubIndex(Car_prices, 18K, Years) →
Car_type ▶ VW Honda BMW 2007 2005 «null»
If «u» is an array of values, an array of index values is returned.
SubIndex(Car_prices, [18K, 19K] Car_type) →
Years ▶ Subindex ▼ 2005 2006 2007 2008 2009 18K Honda «null» VW «null» «null» 19K «null» Honda «null» VW «null»
PositionInIndex(a, x, i)
Returns the position in index «i» — that is, a number from 1 to the size of index «i» — of the last element of array «a» equal to «x»; if no element is equal, it returns 0. See also PositionInIndex().
When array «a» is multidimensional, the result is reduced by one dimension, dimension «i».
Library: Array
Example: When the array is one-dimensional:
Index I := ['A', 'B', 'C']
Variable A := Array(I, [1, 2, 2])
PositionInIndex(A, 1, I) → 1
PositionInIndex(A, 2, I) → 3
PositionInIndex(A, 5, I) → 0
@[i = x]
(see @: Index Position Operator) returns the same result as PositionInIndex(, x, i)
:
PositionInIndex(, 'B', I) → 2
@[I = 'B'] → 2
PositionInIndex(, 'D', I) → 0
@[I = 'D'] → 0
More examples and tips: When the array is multidimensional: Taking the same example from above:
PositionInIndex(Car_prices, 18K, Car_type) →
Years ▶ 2005 2006 2007 2008 2009 2 0 1 0 0
Area(y, x, x1, x2, i)
Returns the area (sum of trapezoids) under the piecewise-linear curve denoted by the points («xi», «yi»), landing in the region «x1» ≤ «x» ≤ «x2». The arrays «x» and «y» must share the common index «i», or when either «x» or «y» is itself an index, «i» can be safely omitted. «x» and «x2» are optional; if they are not specified, the area is calculated across all values of «x».
If «x1» or «x2» fall outside the range of values in «i», the first value (for «x1») or last value (for «x1») are used. Area() computes the total integral across «x», returning a value with one less dimension than «y». Compare Area() to Integrate().
Library: Array
Example
Area(Cost_of_ownership, Time, 0, 2) →
Car_type ▶ VW Honda BMW 5905 7563 6591
See Also
- Sum()
- Product()
- Average()
- Functions Min and Max
- Max()
- Min()
- ArgMax()
- ArgMin()
- CondMin()
- CondMax()
- Subindex()
- PositionInIndex()
- Area()
Enable comment auto-refresher