Difference between revisions of "Subscript and slice of a subarray"
(13 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
<breadcrumbs>Analytica User Guide > Arrays and Indexes > {{PAGENAME}}</breadcrumbs> | <breadcrumbs>Analytica User Guide > Arrays and Indexes > {{PAGENAME}}</breadcrumbs> | ||
+ | ==Overview== | ||
+ | These constructs and functions let you select a [[slice]] or subarray out of an array. | ||
− | + | == x[i = v]: Subscript construct == | |
− | |||
− | == x[i=v]: Subscript construct == | ||
This is the most common method to extract a subarray: | This is the most common method to extract a subarray: | ||
− | :<code>x[i <nowiki>= v]</nowiki></code> | + | :<code>x[i <nowiki> = v]</nowiki></code> |
− | It returns the subarray of | + | It returns the subarray of «x» for which index «i» has value «v». If «v» is not a value of index «i», it returns [[Null]], and usually gives a warning. |
− | If | + | If «x» does not have «i» as a index, it just returns «x». The reason is that if an array «x» is not indexed by «i», it means «x» is constant over all values of «i». |
− | You can apply the subscript construct to an expression, simply by putting the square bracket immediately after the expression: | + | You can apply the [[subscript]] construct to an expression, simply by putting the square bracket immediately after the expression: |
:<code>(Revenue - Cost)[Time = 2010]</code> | :<code>(Revenue - Cost)[Time = 2010]</code> | ||
'''Indexing by name not position''': You can subscript over multiple dimensions, for example: | '''Indexing by name not position''': You can subscript over multiple dimensions, for example: | ||
− | :<code>x[i=v, j=u]</code> | + | :<code>x[i = v, j = u]</code> |
The ordering of the indexes is arbitrary, so you get the same result from: | The ordering of the indexes is arbitrary, so you get the same result from: | ||
− | :<code>x[j=u, i=v]</code> | + | :<code>x[j = u, i = v]</code> |
Indexing by name means that you don’t have to remember or use any intrinsic ordering of indexes in an array, such as rows or columns, inner or outer, common to most computer languages. | Indexing by name means that you don’t have to remember or use any intrinsic ordering of indexes in an array, such as rows or columns, inner or outer, common to most computer languages. | ||
− | The value | + | The value «v» can be an array with some index other than «i» of values from the index «i». For example, «v» might be a subset of «i». In that case, the result is an array with the index(es) of «v» containing the corresponding elements of «x». |
== Subscript(x, i, v) == | == Subscript(x, i, v) == | ||
− | + | The [[Subscript]] function is identical to the subscript construct [[Subscript-Slice_Operator|x[i = v]]], using different syntax. | |
− | == x[@i=n]: Slice construct == | + | == x[@i = n]: Slice construct == |
− | The slice construct has an @ sign before the index. It is different from the subscript construct in that it refers to the numerical ''position ''rather than associating the ''value ''of index | + | The slice construct has an @ sign before the index. It is different from the subscript construct in that it refers to the numerical ''position ''rather than associating the ''value ''of index «i». It returns the «n»'th slice of «x» over index «i»: |
− | :<code>x[@i=n]</code> | + | :<code>x[@i = n]</code> |
− | The number | + | The number «n» should be an integer between 1 (for the first element of index «i») and [[Size]](i) for the last element of «i». If «n» is not an integer in this range, it returns [[Null]], and returns a warning (unless warnings have been turned off). |
Like the subscript construct, it can slice over multiple indexes, for example: | Like the subscript construct, it can slice over multiple indexes, for example: | ||
− | :<code>x[@i=n, @j=m]</code> | + | :<code>x[@i = n, @j = m]</code> |
And also like the subscript construct, the ordering of the indexes is arbitrary. | And also like the subscript construct, the ordering of the indexes is arbitrary. | ||
Line 47: | Line 47: | ||
'''Mixing subscript and slice constructs''': You can mix slice and subscript operations in the same expression in any order: | '''Mixing subscript and slice constructs''': You can mix slice and subscript operations in the same expression in any order: | ||
− | :<code>x[@i=1, j=2, k=3]</code> | + | :<code>x[@i = 1, j = 2, k = 3]</code> |
==Slice(x, i, v)== | ==Slice(x, i, v)== | ||
− | + | The [[Slice]] function is identical to the slice construct [[Subscript-Slice_Operator|x[@i=v]]], using different syntax. | |
− | == Slice(x, n) == | + | ==Slice(x, n) == |
− | If | + | If [[Slice]]() has only two parameters, and «x» has a single dimension, it returns the «n»'th element of «x». For example: |
:<code>Index Quarters := 'Q' & 1..4 </code> | :<code>Index Quarters := 'Q' & 1..4 </code> | ||
Line 62: | Line 62: | ||
:<code>Slice(2000..2003, 4) → 2003</code> | :<code>Slice(2000..2003, 4) → 2003</code> | ||
− | It also works to get the | + | It also works to get the «n»'th slice of a multidimensional array over an unindexed dimension, for example: |
:<code>Slice(Quarters & ' ' & 2000..2003, 4) → Array(Quarters, ['Q1 2003', 'Q2 2003', 'Q3 2003', 'Q4 2003'])</code> | :<code>Slice(Quarters & ' ' & 2000..2003, 4) → Array(Quarters, ['Q1 2003', 'Q2 2003', 'Q3 2003', 'Q4 2003'])</code> | ||
− | <tip title="Tip">If | + | <tip title="Tip">If «x» is a scalar, or if «x» is an array with two or more indexed dimensions and no unindexed dimensions, [[Slice]](x, n) simply returns «x».</tip> |
===Library=== | ===Library=== | ||
Line 74: | Line 74: | ||
Here, Analytica returns the values in '''Cost '''corresponding to the first element in '''Car_type''', that is, the values of '''VW''': | Here, Analytica returns the values in '''Cost '''corresponding to the first element in '''Car_type''', that is, the values of '''VW''': | ||
− | <code>Slice(Cost, Car_type, 1) | + | :<code>Slice(Cost, Car_type, 1) →</code> |
− | {| class="wikitable" | + | :{| class="wikitable" |
− | |||
! colspan="3" |Mpg ▶ | ! colspan="3" |Mpg ▶ | ||
|- | |- | ||
− | |||
!26 | !26 | ||
!30 | !30 | ||
!35 | !35 | ||
|- | |- | ||
− | |||
|2185 | |2185 | ||
|1705 | |1705 | ||
|1585 | |1585 | ||
|} | |} | ||
− | Here, | + | Here, «n» is an array of positions: |
− | <code>Slice(Cost, Car_type, [1,2]) | + | :<code>Slice(Cost, Car_type, [1, 2]) →</code> |
− | {| class="wikitable" | + | :{| class="wikitable" |
! | ! | ||
! colspan="3" |Mpg ▶ | ! colspan="3" |Mpg ▶ | ||
Line 112: | Line 109: | ||
|} | |} | ||
− | ==Preceding time slice: x[Time-1]== | + | ==Preceding time slice: x[Time - 1]== |
− | '''x[Time-n] '''refers to the built-in index <code>Time</code>. It returns the value of variable | + | '''x[Time - n] '''refers to the built-in index <code>Time</code>. It returns the value of variable «x» for the time period that is «n» periods prior to the current time period. This function is only valid inside the [[Dynamic]]() function |
== @: Index Position Operator == | == @: Index Position Operator == | ||
− | The '''''position '''''of value | + | The '''''position '''''of value «x» in an index «i» is the integer «n» where «x» is the «n»'th element of «i». «n» is a number between 1 and [[Size]](i). The first element of «i» is at position 1; the last element of «i» is at position [[Size]](i). The [[Index_Position_Operator%3A%3A@|position operator @]] offers three ways to work with positions: |
− | * | + | * [[Index_Position_Operator%3A%3A@|@i]] → an array of integers from 1 to [[Size]](i) indexed by «i». |
− | * | + | * [[Index_Position_Operator%3A%3A@|@[i = x]]] → the position of value «x» in index «i» or 0 if «x» is not an element of «i». |
− | * | + | * [[Index_Position_Operator%3A%3A@|e[@i = n]]] → the «n»'th [[slice]] of the value of expression «e» over index «i». |
===Examples=== | ===Examples=== | ||
− | <code>Index Car_type :=</code> | + | :<code>Index Car_type :=</code> |
− | {| class="wikitable" | + | :{| class="wikitable" |
|VW | |VW | ||
|Honda | |Honda | ||
|BMW | |BMW | ||
|} | |} | ||
− | <code>@Car_type | + | |
− | {| class="wikitable" | + | :<code>@Car_type →</code> |
− | + | :{| class="wikitable" | |
! colspan="3" |Car_type ▶ | ! colspan="3" |Car_type ▶ | ||
|- | |- | ||
− | |||
!VW | !VW | ||
!Honda | !Honda | ||
!BMW | !BMW | ||
|- | |- | ||
− | |||
|1 | |1 | ||
|2 | |2 | ||
|3 | |3 | ||
|} | |} | ||
− | |||
− | <code>Car_type[@Car_type=2] | + | :<code>@[Car_type='Honda'] → 2</code> |
+ | :<code>Car_type[@Car_type=2] → 'Honda'</code> | ||
===More examples and tips=== | ===More examples and tips=== | ||
− | <code>Index Time:</code> | + | :<code>Index Time:</code> |
− | {| class="wikitable" | + | :{| class="wikitable" |
|0 | |0 | ||
|1 | |1 | ||
Line 157: | Line 152: | ||
|} | |} | ||
− | <code>Years := Time+2007 | + | :<code>Years := Time + 2007 →:</code> |
− | {| class=" | + | :{| class="wikitable" |
|2007 | |2007 | ||
|2008 | |2008 | ||
Line 166: | Line 161: | ||
|} | |} | ||
− | <code>@Time | + | :<code>@Time →</code> |
− | {| class="wikitable" | + | :{| class="wikitable" |
− | |||
! colspan="5" |Time ▶ | ! colspan="5" |Time ▶ | ||
|- | |- | ||
− | |||
!0 | !0 | ||
!1 | !1 | ||
Line 178: | Line 171: | ||
!4 | !4 | ||
|- | |- | ||
− | |||
|1 | |1 | ||
|2 | |2 | ||
Line 186: | Line 178: | ||
|} | |} | ||
− | <code>@[Time=2] | + | :<code>@[Time = 2] → 3</code> |
− | + | :<code>@Time = 3 →</code> | |
− | <code>@Time=3 | + | :{| class="wikitable" |
− | {| class="wikitable" | ||
− | |||
! colspan="5" |Time ▶ | ! colspan="5" |Time ▶ | ||
|- | |- | ||
− | |||
!0 | !0 | ||
!1 | !1 | ||
Line 200: | Line 189: | ||
!4 | !4 | ||
|- | |- | ||
− | |||
|0 | |0 | ||
|0 | |0 | ||
Line 207: | Line 195: | ||
|0 | |0 | ||
|} | |} | ||
− | |||
− | <code>(Time+2007)[@Time=3] | + | :<code>Time[@Time = 3] → 2</code> |
+ | :<code>(Time + 2007)[@Time = 3] → 2009</code> | ||
− | <tip title="Tip">You can use this operator to re-index an array by another index having the same length but different elements. For example, suppose <code>Revenue</code> is indexed by | + | <tip title="Tip">You can use this operator to re-index an array by another index having the same length but different elements. For example, suppose <code>Revenue</code> is indexed by [[Time]], this following gives the same array but indexed by '''Years''': <code>Revenue[@Time = @Years]</code></tip> |
==Choice(i, n'', inclAll, eval, result, separator'')== | ==Choice(i, n'', inclAll, eval, result, separator'')== | ||
− | Appears as a popup menu in the definition field, allowing selection of the | + | Appears as a popup menu in the definition field, allowing selection of the «n»'th item from «i». [[Choice]]() must appear at the topmost level of a definition or table cell. It cannot be used inside another expression. The optional «inclAll» parameter controls whether the "All" option («n» = 0) appears on the popup («inclAll» defaults to True). When you select '''All''', Analytica runs the model on all inputs so you can see how your result varies as this parameter varies, a technique commonly referred to as ''parametric analysis''. If you don’t want '''All '''to be an option, specify «inclAll» as <code>false</code>. |
− | When using | + | When using [[Choice]] in a table cell, it is a good practice to set «inclAll» to <code>false</code>. |
− | In a choice definition (non-table), you can specify | + | In a choice definition (non-table), you can specify «i» to be [[Self]] if the [[Domain attribute]] contains an explicit list. Otherwise, «i» is an index that contains the list of options. «n» is the currently selected item, with 1 being the first item, or it is 0 for the '''All '''option. When the user makes a new selection, Analytica will rewrite the definition by changing «n» to a new number. «n» must be a literal number, it cannot be a general expression. |
− | When index | + | When index «i» contains [[handle]]s to objects, the optional boolean «eval» parameter controls whether the handle is returned (<code>eval: false</code>) or the result of evaluating the variable is returned (<code>eval: true</code>). The optional «result» parameter can be an array indexed by «i», which specifies the value returned and which may be different from the elements of «i». You can include separators in the choice menu by specifying a value for the optional «separator» parameter. The elements of «i» matching «separator» will appear as non-selectable separators in the popup menu. |
===Examples=== | ===Examples=== | ||
− | <code>Choice(Years, 2) | + | :<code>Choice(Years, 2) → 1986</code> |
− | If | + | If «n» = 0, and «inclAll» is true, it returns all values of «i»: |
− | <code>Choice(Years, 0, 1) | + | :<code>Choice(Years, 0, 1) →</code> |
− | {| class="wikitable" | + | :{| class="wikitable" |
− | |||
! colspan="4" |Years ▶ | ! colspan="4" |Years ▶ | ||
|- | |- | ||
− | |||
|1985 | |1985 | ||
|1986 | |1986 | ||
Line 239: | Line 225: | ||
|} | |} | ||
− | [[ | + | ==See Also== |
+ | * [[Slice]] | ||
+ | * [[Slice assignment]] | ||
+ | * [[Subscript-Slice Operator]] | ||
− | + | ||
− | <footer>Splice a table when computed indexes change / {{PAGENAME}} / Choice menus in an edit table</footer> | + | <footer>Splice a table when computed indexes change / {{PAGENAME}} / Choice menus and Checkboxes in an edit table</footer> |
Latest revision as of 21:05, 16 May 2016
Overview
These constructs and functions let you select a slice or subarray out of an array.
x[i = v]: Subscript construct
This is the most common method to extract a subarray:
x[i = v]
It returns the subarray of «x» for which index «i» has value «v». If «v» is not a value of index «i», it returns Null, and usually gives a warning.
If «x» does not have «i» as a index, it just returns «x». The reason is that if an array «x» is not indexed by «i», it means «x» is constant over all values of «i».
You can apply the subscript construct to an expression, simply by putting the square bracket immediately after the expression:
(Revenue - Cost)[Time = 2010]
Indexing by name not position: You can subscript over multiple dimensions, for example:
x[i = v, j = u]
The ordering of the indexes is arbitrary, so you get the same result from:
x[j = u, i = v]
Indexing by name means that you don’t have to remember or use any intrinsic ordering of indexes in an array, such as rows or columns, inner or outer, common to most computer languages.
The value «v» can be an array with some index other than «i» of values from the index «i». For example, «v» might be a subset of «i». In that case, the result is an array with the index(es) of «v» containing the corresponding elements of «x».
Subscript(x, i, v)
The Subscript function is identical to the subscript construct x[i = v], using different syntax.
x[@i = n]: Slice construct
The slice construct has an @ sign before the index. It is different from the subscript construct in that it refers to the numerical position rather than associating the value of index «i». It returns the «n»'th slice of «x» over index «i»:
x[@i = n]
The number «n» should be an integer between 1 (for the first element of index «i») and Size(i) for the last element of «i». If «n» is not an integer in this range, it returns Null, and returns a warning (unless warnings have been turned off).
Like the subscript construct, it can slice over multiple indexes, for example:
x[@i = n, @j = m]
And also like the subscript construct, the ordering of the indexes is arbitrary.
Mixing subscript and slice constructs: You can mix slice and subscript operations in the same expression in any order:
x[@i = 1, j = 2, k = 3]
Slice(x, i, v)
The Slice function is identical to the slice construct x[@i=v], using different syntax.
Slice(x, n)
If Slice() has only two parameters, and «x» has a single dimension, it returns the «n»'th element of «x». For example:
Index Quarters := 'Q' & 1..4
Slice(Quarters, 2) → 'Q2'
This method is the only way to extract an element from an unindexed array, for example:
Slice(2000..2003, 4) → 2003
It also works to get the «n»'th slice of a multidimensional array over an unindexed dimension, for example:
Slice(Quarters & ' ' & 2000..2003, 4) → Array(Quarters, ['Q1 2003', 'Q2 2003', 'Q3 2003', 'Q4 2003'])
Library
Array
Examples
Here, Analytica returns the values in Cost corresponding to the first element in Car_type, that is, the values of VW:
Slice(Cost, Car_type, 1) →
Mpg ▶ 26 30 35 2185 1705 1585
Here, «n» is an array of positions:
Slice(Cost, Car_type, [1, 2]) →
Mpg ▶ 26 30 35 1 2185 1705 1585 2 2810 2330 2210
Preceding time slice: x[Time - 1]
x[Time - n] refers to the built-in index Time
. It returns the value of variable «x» for the time period that is «n» periods prior to the current time period. This function is only valid inside the Dynamic() function
@: Index Position Operator
The position of value «x» in an index «i» is the integer «n» where «x» is the «n»'th element of «i». «n» is a number between 1 and Size(i). The first element of «i» is at position 1; the last element of «i» is at position Size(i). The position operator @ offers three ways to work with positions:
- @i → an array of integers from 1 to Size(i) indexed by «i».
- @[i = x] → the position of value «x» in index «i» or 0 if «x» is not an element of «i».
- e[@i = n] → the «n»'th slice of the value of expression «e» over index «i».
Examples
Index Car_type :=
VW Honda BMW
@Car_type →
Car_type ▶ VW Honda BMW 1 2 3
@[Car_type='Honda'] → 2
Car_type[@Car_type=2] → 'Honda'
More examples and tips
Index Time:
0 1 2 3 4
Years := Time + 2007 →:
2007 2008 2009 2010 2011
@Time →
Time ▶ 0 1 2 3 4 1 2 3 4 5
@[Time = 2] → 3
@Time = 3 →
Time ▶ 0 1 2 3 4 0 0 1 0 0
Time[@Time = 3] → 2
(Time + 2007)[@Time = 3] → 2009
Revenue
is indexed by Time, this following gives the same array but indexed by Years: Revenue[@Time = @Years]
Choice(i, n, inclAll, eval, result, separator)
Appears as a popup menu in the definition field, allowing selection of the «n»'th item from «i». Choice() must appear at the topmost level of a definition or table cell. It cannot be used inside another expression. The optional «inclAll» parameter controls whether the "All" option («n» = 0) appears on the popup («inclAll» defaults to True). When you select All, Analytica runs the model on all inputs so you can see how your result varies as this parameter varies, a technique commonly referred to as parametric analysis. If you don’t want All to be an option, specify «inclAll» as false
.
When using Choice in a table cell, it is a good practice to set «inclAll» to false
.
In a choice definition (non-table), you can specify «i» to be Self if the Domain attribute contains an explicit list. Otherwise, «i» is an index that contains the list of options. «n» is the currently selected item, with 1 being the first item, or it is 0 for the All option. When the user makes a new selection, Analytica will rewrite the definition by changing «n» to a new number. «n» must be a literal number, it cannot be a general expression.
When index «i» contains handles to objects, the optional boolean «eval» parameter controls whether the handle is returned (eval: false
) or the result of evaluating the variable is returned (eval: true
). The optional «result» parameter can be an array indexed by «i», which specifies the value returned and which may be different from the elements of «i». You can include separators in the choice menu by specifying a value for the optional «separator» parameter. The elements of «i» matching «separator» will appear as non-selectable separators in the popup menu.
Examples
Choice(Years, 2) → 1986
If «n» = 0, and «inclAll» is true, it returns all values of «i»:
Choice(Years, 0, 1) →
Years ▶ 1985 1986 1987 1988
See Also
Enable comment auto-refresher