Difference between revisions of "SubIndex"

(StepInterp added to See Also)
 
(19 intermediate revisions by 3 users not shown)
Line 2: Line 2:
 
[[Category:Doc Status D]]  
 
[[Category:Doc Status D]]  
 
[[Category:Array Library]]
 
[[Category:Array Library]]
<!-- For Lumina use, do not change -->
+
{{ReleaseBar}}
 +
 
 +
SubIndex is used to find a value in an array.
 
   
 
   
 +
== SubIndex(a, u, i{{Release|5.0||'', first''}}) ==
 +
Assuming array «a» has index «i», it returns the value of «i» for which «a» [«i»] = «u». If  there is no match for «u» in «a», it returns [[Null]].  If there are multiple matches, it returns the last value of «i». 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.
  
== SubIndex(a, u, i) ==
+
(''New to [[Analytica 5.2]]'') When «u» occurs more than once in «a», it finds the last occurrence unless the optional «first» parameter is true.  
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.
+
Subindex is similar to Vlookup and Hlookup in Excel.   
  
== Array Abstraction ==
+
[[SubIndex]] is the inverse of [[SubScript]]:  Where [[SubScript]] maps from the index value to the array value, [[SubIndex]] maps from the array value to the index value.  [[SubIndex]] is not as efficient as [[SubScript]] -- [[SubScript]] uses a hash-table for constant-time associative lookup, but [[SubIndex]] must search the array  for the indicated value, which takes time  linear in the size of the array.
  
 +
== Example==
 +
:<code>Variable A :=</code>
 +
:{| class="wikitable"
 +
!! colspan="6" | I &#9654;
 +
|-
 +
!|''''a''''
 +
!|''''b''''
 +
!|''''c''''
 +
!|''''d''''
 +
!|''''e''''
 +
!|''''f''''
 +
|-
 +
|4
 +
|3
 +
|2
 +
|5
 +
|3
 +
|1
 +
|}
 +
 +
:<code>SubIndex(A, 2, I) &rarr; 'c'</code>
 +
:<code>SubIndex(A, 3, I) &rarr; 'e'</code>
 +
:<code>SubIndex(A, 6, I) &rarr; «null»</code>
 +
:<code>SubIndex(A, 1..5, I) &rarr; ['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.
  
== Examples ==
+
=== Example 1===
Let:
+
Define variable '''A''' as above and let:
  
{| class="wikitable"
+
:<code>Variable B :=</code>
! I &rarr; !! 'a' !! 'b' !! 'c' !! 'd' !! 'e' !! 'f'
+
:{| class="wikitable"
 +
! !! colspan="6" | I &#9654;
 
|-
 
|-
! A &rarr;  
+
|'''J &#9660;'''
| 4 || 3 || 2 || 5 || 3 || 1
+
|''''a''''
 +
|''''b''''
 +
|''''c''''
 +
|''''d''''
 +
|''''e''''
 +
|''''f''''
 +
|-
 +
|'''1'''
 +
|3
 +
|5
 +
|2
 +
|4
 +
|5
 +
|3
 +
|-
 +
|'''2'''
 +
|2
 +
|1
 +
|5
 +
|2
 +
|3
 +
|4
 
|}
 
|}
  
{| class="wikitable"
+
:<code>Variable C :=</code>
! colspan="2" rowspan="2" | B
+
:{| class="wikitable"
! colspan="6" | I
+
!! colspan="5" | K &#9654;
|-
 
! 'a' !! 'b' !! 'c' !! 'd' !! 'e' !! 'f'
 
 
|-
 
|-
! rowspan="2" | J
+
!|'''1'''
! 1
+
!|'''2'''
| 3 || 5 || 2 || 4 || 5 || 3
+
!|'''3'''
 +
!|'''4'''
 +
!|'''5'''
 
|-
 
|-
! 2
+
|4
| 2 || 1 || 5 || 2 || 3 || 4
+
|2
 +
|5
 +
|1
 +
|3
 
|}
 
|}
  
{| class="wikitable"
+
Then:
! K &rarr; !! 1 !! 2 !! 3 !! 4 !! 5
+
:<code>SubIndex(B, 2, I) &rarr;</code>
 +
:{| class="wikitable"
 +
! colspan="2" |J &#9654;
 +
|-
 +
!1
 +
!2
 
|-
 
|-
! C &rarr;
+
|'c'
| 4 || 2 || 5 || 1 || 3
+
|'d'
 
|}
 
|}
  
 
+
:<code>SubIndex(A, C, I) &rarr;</code>
Then:
+
:{| class="wikitable"
SubIndex(A,2,I) &rarr; 'c'
 
SubIndex(A,3,I) &rarr; 'e'
 
SubIndex(A,6,I) &rarr; «null»
 
SubIndex(A,1..5,I) &rarr; ['f', 'c', 'e', 'a', 'd']
 
:
 
SubIndex(A,C,I) &rarr;
 
{| class="wikitable"
 
 
! colspan="5" |K &#9654;
 
! colspan="5" |K &#9654;
 
|-
 
|-
Line 68: Line 121:
 
|'e'
 
|'e'
 
|}
 
|}
  SubIndex(B,2,I) &rarr;  
+
 
{| class="wikitable"
+
If «u» is an array of values, in this case the variable '''C''', an array is returned:
! colspan="2" |J &#9654;
+
 
 +
:<code>SubIndex(B, C, I) &rarr;</code>
 +
:{| class="wikitable"
 +
!
 +
! colspan="6" | K &#9654;
 +
|-
 +
!J &#9660;''
 +
! 1 !! 2 !! 3 !! 4 !! 5
 
|-
 
|-
!1
+
! 1  
!2
+
| 'd' || 'c' || 'e' || «null» || 'f'
 
|-
 
|-
|'c'
+
! 2
|'d'
+
| 'f' || 'd' || 'c' || 'd' || 'e'
 
|}
 
|}
  
The following two examples can be found in the [[User Guide Examples]].
+
===Example 2===
 +
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"  
{| border="1"  
+
! !! colspan="5" | Years &#9654;
! !! colspan="5" style="text-align: left;" | Years &#9654;
 
 
|-
 
|-
 
! style="width:100px;" |'''Car_type &#9660; '''
 
! style="width:100px;" |'''Car_type &#9660; '''
! style="width:75px;" |'''2005           '''
+
! style="width:75px;" |'''2005       '''
! style="width:75px;" |'''2006           '''
+
! style="width:75px;" |'''2006       '''
! style="width:75px;" |'''2007           '''
+
! style="width:75px;" |'''2007       '''
! style="width:75px;" |'''2008           '''
+
! style="width:75px;" |'''2008       '''
! style="width:75px;" |'''2009           '''
+
! style="width:75px;" |'''2009       '''
 
|-
 
|-
 
!'''VW'''  
 
!'''VW'''  
Line 116: Line 176:
 
|}
 
|}
  
<code>Index Car_type := ['VW', 'Honda', 'BMW']</code>
+
:<code>Index Car_type := ['VW', 'Honda', 'BMW']</code>
  
 
Then:
 
Then:
 
+
:<code>SubIndex(Car_prices, 18K, Car_type) &rarr;</code>
SubIndex(Car_prices, 18K, Car_type) &rarr;
+
:{| class="wikitable"
{| class="wikitable"
 
 
! colspan="5" |Years &#9654;
 
! colspan="5" |Years &#9654;
 
|-
 
|-
Line 137: Line 196:
 
|}
 
|}
  
SubIndex(Car_prices, 18K, Years) &rarr;  
+
:<code>SubIndex(Car_prices, 18K, Years) &rarr;</code>
{| class="wikitable"
+
:{| class="wikitable"
 
! colspan="3" |Car_type &#9654;
 
! colspan="3" |Car_type &#9654;
 
|-
 
|-
Line 150: Line 209:
 
|}
 
|}
  
If «u» is an array of values, an array of index values is returned:
+
:<code>SubIndex(Car_prices, [18K, 19K] Car_type) &rarr;</code>
 
+
:{| class="wikitable"
SubIndex(B,C,I) &rarr;
 
{| class="wikitable"
 
! colspan="2" rowspan="2" | B
 
! colspan="6" | K
 
|-
 
! 1 !! 2 !! 3 !! 4 !! 5
 
|-
 
! rowspan="2" | J
 
! 1
 
| 'd' || 'c' || 'e' || «null» || 'f'
 
|-
 
! 2
 
| 'f' || 'd' || 'c' || 'd' || 'e'
 
|}
 
 
 
This example can also be found in the [[User Guide Examples]]; the variables are defined above.
 
 
 
SubIndex(Car_prices, [18K, 19K] Car_type) &rarr;
 
{| class="wikitable"
 
!
 
 
! colspan="5" |Years &#9654;
 
! colspan="5" |Years &#9654;
 
|-
 
|-
!Subindex &#9660;
 
 
!2005
 
!2005
 
!2006
 
!2006
Line 181: Line 219:
 
!2009
 
!2009
 
|-
 
|-
|'''18K'''
 
 
|Honda
 
|Honda
 
|«null»
 
|«null»
Line 188: Line 225:
 
|«null»
 
|«null»
 
|-
 
|-
|'''19K'''
 
 
|«null»
 
|«null»
 
|Honda
 
|Honda
Line 197: Line 233:
  
 
== See Also ==
 
== See Also ==
 
 
* [[PositionInIndex]]
 
* [[PositionInIndex]]
 
* [[Subscript]]
 
* [[Subscript]]
*[[User Guide Examples]] / [[Media: Array Function Examples.ana | Array Function Examples.ana]] / Reducing Functions Module
+
* [[StepInterp]] -- Use this when the exact value of «u» isn't in «a», but you need to find which interval it lands in.
 +
* [[Array-reducing functions]]
 +
*[[User_guide_Examples_folder|User Guide Examples]] / [[Media: Array Function Examples.ana | Array Function Examples.ana]] / Reducing Functions Module

Latest revision as of 16:51, 2 November 2021



Release:

4.6  •  5.0  •  5.1  •  5.2  •  5.3  •  5.4  •  6.0  •  6.1  •  6.2  •  6.3  •  6.4  •  6.5


SubIndex is used to find a value in an array.

SubIndex(a, u, i, first)

Assuming array «a» has index «i», it returns the value of «i» for which «a» [«i»] = «u». If there is no match for «u» in «a», it returns Null. If there are multiple matches, it returns the last value of «i». 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.

(New to Analytica 5.2) When «u» occurs more than once in «a», it finds the last occurrence unless the optional «first» parameter is true.

Subindex is similar to Vlookup and Hlookup in Excel.

SubIndex is the inverse of SubScript: Where SubScript maps from the index value to the array value, SubIndex maps from the array value to the index value. SubIndex is not as efficient as SubScript -- SubScript uses a hash-table for constant-time associative lookup, but SubIndex must search the array for the indicated value, which takes time linear in the size of the array.

Example

Variable A :=
I ▶
'a' 'b' 'c' 'd' 'e' 'f'
4 3 2 5 3 1
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, in this case the variable C, an array is returned:

SubIndex(B, C, I) →
K ▶
J ▼ 1 2 3 4 5
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 ▶
2005 2006 2007 2008 2009
Honda «null» VW «null» «null»
«null» Honda «null» VW «null»

See Also

Comments


You are not allowed to post comments.