Difference between revisions of "PositionInIndex"
Line 5: | Line 5: | ||
== PositionInIndex(a, x, i) == | == 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. When array «a» is multidimensional, the result is reduced by one dimension, dimension «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. When array «a» is multidimensional, the result is reduced by one dimension, dimension «i». The formal declaration of the function is <code>PositionInIndex(a: optional Array[I]; x: atomic; i: IndexType)</code>. |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
[[PositionInIndex]] is the positional equivalent of [[SubIndex]]. You may need [[PositionInIndex]] when «i» contains duplicate values, in which case [[SubIndex]] isn't sufficient because it returns an ambiguous result. | [[PositionInIndex]] is the positional equivalent of [[SubIndex]]. You may need [[PositionInIndex]] when «i» contains duplicate values, in which case [[SubIndex]] isn't sufficient because it returns an ambiguous result. | ||
Line 18: | Line 11: | ||
[[PositionInIndex]] is essentially the inverse of the [[Slice]] function. While [[Slice]] maps from an index position to the corresponding array value, [[PositionInIndex]] maps from the array value to the index position. | [[PositionInIndex]] is essentially the inverse of the [[Slice]] function. While [[Slice]] maps from an index position to the corresponding array value, [[PositionInIndex]] maps from the array value to the index position. | ||
− | |||
− | |||
− | == | + | == Example == |
+ | Let: | ||
+ | |||
+ | <code>Index I := ['A', 'B', 'C']</code> | ||
+ | |||
+ | <code>Variable A := Array(I, [1, 2, 2])</code> | ||
+ | |||
+ | Then: | ||
+ | |||
+ | <code>PositionInIndex(A, 1, I) → 1</code> | ||
+ | |||
+ | <code>PositionInIndex(A, 2, I) → 3</code> | ||
+ | <code>PositionInIndex(A, 5, I) → 0</code> | ||
+ | |||
+ | |||
+ | == Optional Parameters == | ||
+ | === ''a'' === | ||
+ | Parameter «a» is optional. When omitted, it returns the position of «x» in the index «i», or 0 if not found. For example, | ||
+ | |||
+ | <code>PositionInIndex(, 'B', I) → 2</code> | ||
+ | |||
+ | <code>PositionInIndex(, 'D', I) → 0</code> | ||
+ | |||
+ | The syntax <code>@[i = x]</code> does the same thing: | ||
+ | |||
+ | <code>@[I = 'B'] → 2</code> | ||
+ | |||
+ | <code>@[I = 'D'] → 0</code> | ||
+ | |||
+ | |||
+ | ==Details & More Examples== | ||
When «a» is a self-indexed table, [[PositionInIndex]](a, x, a) and [[PositionInIndex]](U: x, I: a) are not the same. [[PositionInIndex]]](a, x, a) will find the value in the array «a», while [[PositionInIndex]]](U: x, I: a) finds «u» along the ''index value'' of «a». | When «a» is a self-indexed table, [[PositionInIndex]](a, x, a) and [[PositionInIndex]](U: x, I: a) are not the same. [[PositionInIndex]]](a, x, a) will find the value in the array «a», while [[PositionInIndex]]](U: x, I: a) finds «u» along the ''index value'' of «a». | ||
When «i» is a normal index (not a self-indexed table, and not Time within a dynamic loop), then [[PositionInIndex]](i, x, i) and [[PositionInIndex]](U: x, I: i) are functionally equivalent. However, [[PositionInIndex]](U: x, I: I) has a speed advantage, particularly when «x» is an array, or when many lookups along «i» will be performed. The average lookup time is O(1) for [[PositionInIndex]](U: x, I: i) but O([[Size]](i)) for [[PositionInIndex]](i, x, i). [[Index Position Operator::@|@[i = x] ]] is also O(1). | When «i» is a normal index (not a self-indexed table, and not Time within a dynamic loop), then [[PositionInIndex]](i, x, i) and [[PositionInIndex]](U: x, I: i) are functionally equivalent. However, [[PositionInIndex]](U: x, I: I) has a speed advantage, particularly when «x» is an array, or when many lookups along «i» will be performed. The average lookup time is O(1) for [[PositionInIndex]](U: x, I: i) but O([[Size]](i)) for [[PositionInIndex]](i, x, i). [[Index Position Operator::@|@[i = x] ]] is also O(1). | ||
− | == | + | ===Example 1=== |
− | |||
Let: | Let: | ||
+ | <code>variable A :=</code> | ||
{| class="wikitable" | {| class="wikitable" | ||
− | ! I & | + | !! colspan="6" style="text-align: left;" | I ▶ |
+ | |- | ||
+ | !|''''a'''' | ||
+ | !|''''b'''' | ||
+ | !|''''c'''' | ||
+ | !|''''d'''' | ||
+ | !|''''e'''' | ||
+ | !|''''f'''' | ||
|- | |- | ||
− | + | |4 | |
− | | 4 | + | |3 |
+ | |2 | ||
+ | |5 | ||
+ | |3 | ||
+ | |1 | ||
|} | |} | ||
+ | <code>variable B :=</code> | ||
{| class="wikitable" | {| class="wikitable" | ||
− | ! | + | ! !! colspan="6" | I ▶ |
− | ! colspan=6 | I | ||
|- | |- | ||
− | + | |'''J ▼''' | |
+ | |''''a'''' | ||
+ | |''''b'''' | ||
+ | |''''c'''' | ||
+ | |''''d'''' | ||
+ | |''''e'''' | ||
+ | |''''f'''' | ||
|- | |- | ||
− | + | |'''1''' | |
− | + | |3 | |
− | | 3 | + | |5 |
+ | |2 | ||
+ | |4 | ||
+ | |5 | ||
+ | |3 | ||
|- | |- | ||
− | + | |'''2''' | |
− | | 2 | + | |2 |
+ | |1 | ||
+ | |5 | ||
+ | |2 | ||
+ | |3 | ||
+ | |4 | ||
|} | |} | ||
− | |||
+ | <code>variable C :=</code> | ||
{| class="wikitable" | {| class="wikitable" | ||
− | ! K & | + | !! colspan="5" style="text-align: left;" | K ▶ |
|- | |- | ||
− | ! | + | !|'''1''' |
− | | 4 || 2 | + | !|'''2''' |
+ | !|'''3''' | ||
+ | !|'''4''' | ||
+ | !|'''5''' | ||
+ | |- | ||
+ | |4 | ||
+ | |2 | ||
+ | |5 | ||
+ | |1 | ||
+ | |3 | ||
|} | |} | ||
Then: | Then: | ||
− | + | ||
− | + | <code>PositionInIndex(A, 2, I) → 3</code> | |
− | + | ||
− | + | <code>PositionInIndex(A, 3, I) → 5</code> | |
− | + | ||
− | @[I = 'd'] → 4 | + | <code>PositionInIndex(A, 6, I) → 0</code> |
− | @[I = 'g'] → 0 | + | |
− | PositionInIndex(A, C, I) → | + | <code>PositionInIndex(A, 1..5, I) → [6, 3, 5, 1, 4]</code> |
+ | |||
+ | <code>PositionInIndex( , 'c', I ) → 3</code> | ||
+ | |||
+ | <code>@[I = 'd'] → 4</code> | ||
+ | |||
+ | <code>@[I = 'g'] → 0</code> | ||
+ | |||
+ | <code>PositionInIndex(A, C, I) →</code> | ||
{| class="wikitable" | {| class="wikitable" | ||
! colspan="5" |K ▶ | ! colspan="5" |K ▶ | ||
Line 84: | Line 148: | ||
|5 | |5 | ||
|} | |} | ||
− | PositionInIndex(B, 2, I) → | + | |
+ | <code>PositionInIndex(B, 2, I) →</code> | ||
{| class="wikitable" | {| class="wikitable" | ||
! colspan="2" |J ▶ | ! colspan="2" |J ▶ | ||
Line 96: | Line 161: | ||
When the array is multidimensional: | When the array is multidimensional: | ||
− | + | ||
+ | <code>PositionInIndex(B, C, I) →</code> | ||
{| class="wikitable" | {| class="wikitable" | ||
! colspan="2" rowspan="2" | B | ! colspan="2" rowspan="2" | B | ||
Line 111: | Line 177: | ||
|} | |} | ||
+ | ===Example 2=== | ||
The following example can be found in the [[User Guide Examples]]. | The following example can be found in the [[User Guide Examples]]. | ||
Line 152: | Line 219: | ||
Then: | Then: | ||
− | + | <code>PositionInIndex(Car_prices, 18K, Car_type) →</code> | |
{| class="wikitable" | {| class="wikitable" | ||
! colspan="5" |Years ▶ | ! colspan="5" |Years ▶ | ||
Line 169: | Line 236: | ||
|} | |} | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
== See Also == | == See Also == |
Revision as of 23:41, 7 December 2015
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. When array «a» is multidimensional, the result is reduced by one dimension, dimension «i». The formal declaration of the function is PositionInIndex(a: optional Array[I]; x: atomic; i: IndexType)
.
PositionInIndex is the positional equivalent of SubIndex. You may need PositionInIndex when «i» contains duplicate values, in which case SubIndex isn't sufficient because it returns an ambiguous result.
PositionInIndex is essentially the inverse of the Slice function. While Slice maps from an index position to the corresponding array value, PositionInIndex maps from the array value to the index position.
Example
Let:
Index I := ['A', 'B', 'C']
Variable A := Array(I, [1, 2, 2])
Then:
PositionInIndex(A, 1, I) → 1
PositionInIndex(A, 2, I) → 3
PositionInIndex(A, 5, I) → 0
Optional Parameters
a
Parameter «a» is optional. When omitted, it returns the position of «x» in the index «i», or 0 if not found. For example,
PositionInIndex(, 'B', I) → 2
PositionInIndex(, 'D', I) → 0
The syntax @[i = x]
does the same thing:
@[I = 'B'] → 2
@[I = 'D'] → 0
Details & More Examples
When «a» is a self-indexed table, PositionInIndex(a, x, a) and PositionInIndex(U: x, I: a) are not the same. PositionInIndex](a, x, a) will find the value in the array «a», while PositionInIndex](U: x, I: a) finds «u» along the index value of «a».
When «i» is a normal index (not a self-indexed table, and not Time within a dynamic loop), then PositionInIndex(i, x, i) and PositionInIndex(U: x, I: i) are functionally equivalent. However, PositionInIndex(U: x, I: I) has a speed advantage, particularly when «x» is an array, or when many lookups along «i» will be performed. The average lookup time is O(1) for PositionInIndex(U: x, I: i) but O(Size(i)) for PositionInIndex(i, x, i). @[i = x] is also O(1).
Example 1
Let:
variable A :=
I ▶ | |||||
---|---|---|---|---|---|
'a' | 'b' | 'c' | 'd' | 'e' | 'f' |
4 | 3 | 2 | 5 | 3 | 1 |
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:
PositionInIndex(A, 2, I) → 3
PositionInIndex(A, 3, I) → 5
PositionInIndex(A, 6, I) → 0
PositionInIndex(A, 1..5, I) → [6, 3, 5, 1, 4]
PositionInIndex( , 'c', I ) → 3
@[I = 'd'] → 4
@[I = 'g'] → 0
PositionInIndex(A, C, I) →
K ▶ | ||||
---|---|---|---|---|
1 | 2 | 3 | 4 | 5 |
1 | 3 | 4 | 6 | 5 |
PositionInIndex(B, 2, I) →
J ▶ | |
---|---|
1 | 2 |
3 | 4 |
When the array is multidimensional:
PositionInIndex(B, C, I) →
B | K | ||||||
---|---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | |||
J | 1 | 4 | 3 | 5 | 0 | 6 | |
2 | 6 | 4 | 3 | 4 | 5 |
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:
PositionInIndex(Car_prices, 18K, Car_type) →
Years ▶ | ||||
---|---|---|---|---|
2005 | 2006 | 2007 | 2008 | 2009 |
2 | 0 | 1 | 0 | 0 |
See Also
- SubIndex
- @[I=u]
- Slice
- User Guide Examples / Array Function Examples.ana / Reducing Functions Module
Enable comment auto-refresher