Difference between revisions of "PositionInIndex"

Line 12: Line 12:
  
 
[[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 ==
 
== Example ==
 
Let:
 
Let:
 
+
:<code>Index I := ['A', 'B', 'C']</code>
<code>Index I := ['A', 'B', 'C']</code>
+
:<code>Variable A := Array(I, [1, 2, 2])</code>
 
 
<code>Variable A := Array(I, [1, 2, 2])</code>
 
  
 
Then:
 
Then:
 
+
:<code>PositionInIndex(A, 1, I) &rarr; 1</code>
<code>PositionInIndex(A, 1, I) &rarr; 1</code>
+
:<code>PositionInIndex(A, 2, I) &rarr; 3</code>   
 
+
:<code>PositionInIndex(A, 5, I) &rarr; 0</code>
<code>PositionInIndex(A, 2, I) &rarr; 3</code>   
 
 
 
<code>PositionInIndex(A, 5, I) &rarr; 0</code>
 
 
 
  
 
== Optional Parameters ==
 
== Optional Parameters ==
 
=== ''a'' ===
 
=== ''a'' ===
 
Parameter «a» is optional.  When omitted, it returns the position of «x» in the index «i», or 0 if not found.  For example,
 
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) &rarr; 2</code>   
<code>PositionInIndex(, 'B', I) &rarr; 2</code>   
+
:<code>PositionInIndex(, 'D', I) &rarr; 0</code>
 
 
<code>PositionInIndex(, 'D', I) &rarr; 0</code>
 
  
 
The syntax <code>@[i = x]</code> does the same thing:
 
The syntax <code>@[i = x]</code> does the same thing:
 
+
:<code>@[I = 'B'] &rarr; 2</code>
<code>@[I = 'B'] &rarr; 2</code>
+
:<code>@[I = 'D'] &rarr; 0</code>
 
 
<code>@[I = 'D'] &rarr; 0</code>
 
 
 
  
 
==Details & More Examples==
 
==Details & More Examples==
Line 52: Line 40:
 
===Example 1===
 
===Example 1===
 
Let:
 
Let:
 
+
:<code>Variable A :=</code>
<code>Variable A :=</code>
+
:{| class="wikitable"
{| class="wikitable"
 
 
!! colspan="6" | I &#9654;
 
!! colspan="6" | I &#9654;
 
|-
 
|-
Line 72: Line 59:
 
|}
 
|}
  
<code>Variable B :=</code>
+
:<code>Variable B :=</code>
{| class="wikitable"
+
:{| class="wikitable"
 
! !! colspan="6" | I &#9654;
 
! !! colspan="6" | I &#9654;
 
|-
 
|-
Line 101: Line 88:
 
|}
 
|}
  
<code>Variable C :=</code>
+
:<code>Variable C :=</code>
{| class="wikitable"
+
:{| class="wikitable"
 
!! colspan="5"| K &#9654;
 
!! colspan="5"| K &#9654;
 
|-
 
|-
Line 119: Line 106:
  
 
Then:
 
Then:
 
+
:<code>PositionInIndex(A, 2, I) &rarr; 3</code>
<code>PositionInIndex(A, 2, I) &rarr; 3</code>
+
:<code>PositionInIndex(A, 3, I) &rarr; 5</code>
 
+
:<code>PositionInIndex(A, 6, I) &rarr; 0</code>
<code>PositionInIndex(A, 3, I) &rarr; 5</code>
+
:<code>PositionInIndex(A, 1..5, I) &rarr; [6, 3, 5, 1, 4]</code>
 
+
:<code>PositionInIndex( , 'c', I ) &rarr; 3</code>
<code>PositionInIndex(A, 6, I) &rarr; 0</code>
+
:<code>@[I = 'd'] &rarr; 4</code>
 
+
:<code>@[I = 'g'] &rarr; 0</code>
<code>PositionInIndex(A, 1..5, I) &rarr; [6, 3, 5, 1, 4]</code>
+
:<code>PositionInIndex(A, C, I) &rarr;</code>
 
+
:{| class="wikitable"
<code>PositionInIndex( , 'c', I ) &rarr; 3</code>
 
 
<code>@[I = 'd'] &rarr; 4</code>
 
 
<code>@[I = 'g'] &rarr; 0</code>
 
 
<code>PositionInIndex(A, C, I) &rarr;</code>
 
{| class="wikitable"
 
 
! colspan="5" |K &#9654;
 
! colspan="5" |K &#9654;
 
|-
 
|-
Line 151: Line 130:
 
|}
 
|}
 
   
 
   
<code>PositionInIndex(B, 2, I) &rarr;</code>
+
:<code>PositionInIndex(B, 2, I) &rarr;</code>
{| class="wikitable"
+
:{| class="wikitable"
 
! colspan="2" |J &#9654;
 
! colspan="2" |J &#9654;
 
|-
 
|-
Line 163: Line 142:
  
 
When the array is multidimensional:
 
When the array is multidimensional:
 
+
:<code>PositionInIndex(B, C, I) &rarr;</code>
<code>PositionInIndex(B, C, I) &rarr;</code>
+
:{| class="wikitable"
 
 
{| class="wikitable"
 
 
!
 
!
 
! colspan="6" | K &#9654;
 
! colspan="6" | K &#9654;
Line 181: Line 158:
  
 
===Example 2===
 
===Example 2===
The following example can be found in the [[User Guide Examples]].
+
The following example can be found in the [[User_guide_Examples_folder|User Guide Examples]].
  
 
Let:
 
Let:
 
+
:<code>Variable Car_prices :=</code>
<code>Variable Car_prices :=</code>
+
:{| class="wikitable"
{| class="wikitable"
 
 
! !! colspan="5" | Years &#9654;
 
! !! colspan="5" | Years &#9654;
 
|-
 
|-
Line 218: Line 194:
 
|}
 
|}
  
<code>Index Car_type := ['VW', 'Honda', 'BMW']</code>
+
:<code>Index Car_type := ['VW', 'Honda', 'BMW']</code>
  
 
Then:
 
Then:
 
+
:<code>PositionInIndex(Car_prices, 18K, Car_type) &rarr;</code>
<code>PositionInIndex(Car_prices, 18K, Car_type) &rarr;</code>
+
:{| class="wikitable"
{| class="wikitable"
+
! colspan="5" |Years &#9654;
! colspan="5" |Years
 
 
|-
 
|-
 
!2005
 
!2005
Line 238: Line 213:
 
|0
 
|0
 
|}
 
|}
 
  
 
== See Also ==
 
== See Also ==
Line 244: Line 218:
 
* [[Index Position Operator::@|@[I=u] ]]
 
* [[Index Position Operator::@|@[I=u] ]]
 
* [[Slice]]
 
* [[Slice]]
*[[User Guide Examples]] / [[Media: Array Function Examples.ana | Array Function Examples.ana]] / Reducing Functions Module
+
* [[User_guide_Examples_folder|User Guide Examples]] / [[Media: Array Function Examples.ana | Array Function Examples.ana]] / Reducing Functions Module

Revision as of 22:32, 11 January 2016


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) →
K ▶
J ▼ 1 2 3 4 5
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

Comments


You are not allowed to post comments.