Difference between revisions of "SubIndex"
Line 10: | Line 10: | ||
[[SubIndex]] is the inverse of [[SubScript]]. While [[SubScript]] maps from the index value to the array element, [[SubIndex]] maps from the array element to the index value. [[SubIndex]] is not as efficient as [[SubScript]] -- while [[SubScript]] makes use of a hash-table for constant-time associative lookup, [[SubIndex]] must search the array each time for the indicated value, which takes linear time relative to the size of the array. | [[SubIndex]] is the inverse of [[SubScript]]. While [[SubScript]] maps from the index value to the array element, [[SubIndex]] maps from the array element to the index value. [[SubIndex]] is not as efficient as [[SubScript]] -- while [[SubScript]] makes use of a hash-table for constant-time associative lookup, [[SubIndex]] must search the array each time for the indicated value, which takes linear time relative to the size of the array. | ||
− | == | + | == Examples== |
+ | Let: | ||
+ | |||
+ | <code>variable A :=</code> | ||
+ | {| class="wikitable" | ||
+ | !! colspan="6" | I ▶ | ||
+ | |- | ||
+ | !|''''a'''' | ||
+ | !|''''b'''' | ||
+ | !|''''c'''' | ||
+ | !|''''d'''' | ||
+ | !|''''e'''' | ||
+ | !|''''f'''' | ||
+ | |- | ||
+ | |4 | ||
+ | |3 | ||
+ | |2 | ||
+ | |5 | ||
+ | |3 | ||
+ | |1 | ||
+ | |} | ||
+ | |||
+ | Then: | ||
+ | |||
+ | <code>SubIndex(A, 2, I) → 'c'</code> | ||
+ | |||
+ | <code>SubIndex(A, 3, I) → 'e'</code> | ||
+ | |||
+ | <code>SubIndex(A, 6, I) → «null»</code> | ||
+ | |||
+ | <code>SubIndex(A, 1..5, I) → ['f', 'c', 'e', 'a', 'd']</code> | ||
+ | == Details & More Examples == | ||
+ | === Array abstraction=== | ||
When «a» has more than one dimension, or «u» is an array, [[SubIndex]] array abstracts according to the standard rules of [[Array Abstraction|array abstraction]]. For example, when «a» contains an index other than «i», [[SubIndex]] is applied to each slice of that index, returning the position of «u» in the corresponding slice. When «u» contains an array of values, the position of each of those values in «a» is returned. | When «a» has more than one dimension, or «u» is an array, [[SubIndex]] array abstracts according to the standard rules of [[Array Abstraction|array abstraction]]. For example, when «a» contains an index other than «i», [[SubIndex]] is applied to each slice of that index, returning the position of «u» in the corresponding slice. When «u» contains an array of values, the position of each of those values in «a» is returned. | ||
− | == | + | === Example 1=== |
− | + | Define variable '''A''' as above and let: | |
+ | <code>variable B :=</code> | ||
{| class="wikitable" | {| class="wikitable" | ||
− | ! I & | + | ! !! colspan="6" | I ▶ |
+ | |- | ||
+ | |'''J ▼''' | ||
+ | |''''a'''' | ||
+ | |''''b'''' | ||
+ | |''''c'''' | ||
+ | |''''d'''' | ||
+ | |''''e'''' | ||
+ | |''''f'''' | ||
+ | |- | ||
+ | |'''1''' | ||
+ | |3 | ||
+ | |5 | ||
+ | |2 | ||
+ | |4 | ||
+ | |5 | ||
+ | |3 | ||
|- | |- | ||
− | + | |'''2''' | |
− | + | |2 | |
+ | |1 | ||
+ | |5 | ||
+ | |2 | ||
+ | |3 | ||
+ | |4 | ||
|} | |} | ||
+ | <code>variable C :=</code> | ||
{| class="wikitable" | {| class="wikitable" | ||
− | ! | + | !! colspan="5" | K ▶ |
− | ! colspan=" | ||
− | |||
− | |||
|- | |- | ||
− | ! | + | !|'''1''' |
− | ! | + | !|'''2''' |
− | | 3 | | + | !|'''3''' |
+ | !|'''4''' | ||
+ | !|'''5''' | ||
|- | |- | ||
− | + | |4 | |
− | | 2 || 1 | + | |2 |
+ | |5 | ||
+ | |1 | ||
+ | |3 | ||
|} | |} | ||
+ | Then: | ||
+ | |||
+ | <code>SubIndex(B, 2, I) →</code> | ||
{| class="wikitable" | {| class="wikitable" | ||
− | ! | + | ! colspan="2" |J ▶ |
+ | |- | ||
+ | !1 | ||
+ | !2 | ||
|- | |- | ||
− | + | |'c' | |
− | | | + | |'d' |
|} | |} | ||
− | + | <code>SubIndex(A, C, I) →</code> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
{| class="wikitable" | {| class="wikitable" | ||
! colspan="5" |K ▶ | ! colspan="5" |K ▶ | ||
Line 67: | Line 124: | ||
|'e' | |'e' | ||
|} | |} | ||
− | + | ||
+ | If «u» is an array of values, an array of index values is returned: | ||
+ | |||
+ | <code>SubIndex(B, C, I) →</code> | ||
{| class="wikitable" | {| class="wikitable" | ||
− | ! colspan="2" | | + | ! colspan="2" rowspan="2" | B |
+ | ! colspan="6" | K | ||
+ | |- | ||
+ | ! 1 !! 2 !! 3 !! 4 !! 5 | ||
|- | |- | ||
− | !1 | + | ! rowspan="2" | J |
− | + | ! 1 | |
+ | | 'd' || 'c' || 'e' || «null» || 'f' | ||
|- | |- | ||
− | |'c' | + | ! 2 |
− | |'d' | + | | 'f' || 'd' || 'c' || 'd' || 'e' |
|} | |} | ||
− | The following | + | ===Example 2=== |
+ | The following example can be found in the [[User Guide Examples]]. | ||
Let: | Let: | ||
Line 84: | Line 149: | ||
<code>Variable Car_prices :=</code> | <code>Variable Car_prices :=</code> | ||
{| border="1" | {| border="1" | ||
− | ! !! colspan="5 | + | ! !! colspan="5" | Years ▶ |
|- | |- | ||
! style="width:100px;" |'''Car_type ▼ ''' | ! style="width:100px;" |'''Car_type ▼ ''' | ||
Line 119: | Line 184: | ||
Then: | Then: | ||
− | + | <code>SubIndex(Car_prices, 18K, Car_type) →</code> | |
{| class="wikitable" | {| class="wikitable" | ||
! colspan="5" |Years ▶ | ! colspan="5" |Years ▶ | ||
Line 136: | Line 201: | ||
|} | |} | ||
− | + | <code>SubIndex(Car_prices, 18K, Years) →</code> | |
{| class="wikitable" | {| class="wikitable" | ||
! colspan="3" |Car_type ▶ | ! colspan="3" |Car_type ▶ | ||
Line 149: | Line 214: | ||
|} | |} | ||
− | |||
− | + | <code>SubIndex(Car_prices, [18K, 19K] Car_type) →</code> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
{| class="wikitable" | {| class="wikitable" | ||
! | ! | ||
Line 196: | Line 243: | ||
== See Also == | == See Also == | ||
− | |||
* [[PositionInIndex]] | * [[PositionInIndex]] | ||
* [[Subscript]] | * [[Subscript]] | ||
*[[User Guide Examples]] / [[Media: Array Function Examples.ana | Array Function Examples.ana]] / Reducing Functions Module | *[[User Guide Examples]] / [[Media: Array Function Examples.ana | Array Function Examples.ana]] / Reducing Functions Module |
Revision as of 22:46, 7 December 2015
SubIndex(a, u, i)
Returns the value of index «i» for which array «a» (indexed by «i») is equal to «u». If «u» does not occur in «a», returns Null. If «u» occurs more than once in «a», it returns the last value of «i» that is equal to «u». If «a» has index(es) in addition to «i», or if the value «u» is an array with other indexes, those indexes also appear in the result.
SubIndex is the inverse of SubScript. While SubScript maps from the index value to the array element, SubIndex maps from the array element to the index value. SubIndex is not as efficient as SubScript -- while SubScript makes use of a hash-table for constant-time associative lookup, SubIndex must search the array each time for the indicated value, which takes linear time relative to the size of the array.
Examples
Let:
variable A :=
I ▶ | |||||
---|---|---|---|---|---|
'a' | 'b' | 'c' | 'd' | 'e' | 'f' |
4 | 3 | 2 | 5 | 3 | 1 |
Then:
SubIndex(A, 2, I) → 'c'
SubIndex(A, 3, I) → 'e'
SubIndex(A, 6, I) → «null»
SubIndex(A, 1..5, I) → ['f', 'c', 'e', 'a', 'd']
Details & More Examples
Array abstraction
When «a» has more than one dimension, or «u» is an array, SubIndex array abstracts according to the standard rules of array abstraction. For example, when «a» contains an index other than «i», SubIndex is applied to each slice of that index, returning the position of «u» in the corresponding slice. When «u» contains an array of values, the position of each of those values in «a» is returned.
Example 1
Define variable A as above and let:
variable B :=
I ▶ | ||||||
---|---|---|---|---|---|---|
J ▼ | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' |
1 | 3 | 5 | 2 | 4 | 5 | 3 |
2 | 2 | 1 | 5 | 2 | 3 | 4 |
variable C :=
K ▶ | ||||
---|---|---|---|---|
1 | 2 | 3 | 4 | 5 |
4 | 2 | 5 | 1 | 3 |
Then:
SubIndex(B, 2, I) →
J ▶ | |
---|---|
1 | 2 |
'c' | 'd' |
SubIndex(A, C, I) →
K ▶ | ||||
---|---|---|---|---|
1 | 2 | 3 | 4 | 5 |
'a' | 'c' | 'd' | 'f' | 'e' |
If «u» is an array of values, an array of index values is returned:
SubIndex(B, C, I) →
B | K | ||||||
---|---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | |||
J | 1 | 'd' | 'c' | 'e' | «null» | 'f' | |
2 | 'f' | 'd' | 'c' | 'd' | 'e' |
Example 2
The following example can be found in the User Guide Examples.
Let:
Variable Car_prices :=
Years ▶ | |||||
---|---|---|---|---|---|
Car_type ▼ | 2005 | 2006 | 2007 | 2008 | 2009 |
VW | $16,000 | $17,000 | $18,000 | $19,000 | $20,000 |
Honda | $18,000 | $19,000 | $20,000 | $22,000 | $24,000 |
BMW | $25,000 | $26,000 | $28,000 | $30,000 | $32,000 |
Index Car_type := ['VW', 'Honda', 'BMW']
Then:
SubIndex(Car_prices, 18K, Car_type) →
Years ▶ | ||||
---|---|---|---|---|
2005 | 2006 | 2007 | 2008 | 2009 |
Honda | «null» | VW | «null» | «null» |
SubIndex(Car_prices, 18K, Years) →
Car_type ▶ | ||
---|---|---|
VW | Honda | BMW |
2007 | 2005 | «null» |
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» |
See Also
- PositionInIndex
- Subscript
- User Guide Examples / Array Function Examples.ana / Reducing Functions Module
Enable comment auto-refresher