Difference between revisions of "CopyIndex"

 
m (4.7 -> 5.0)
 
(15 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
[[category:Functions that create lists]]
 
[[category:Functions that create lists]]
{{stub}}
+
[[Category:Doc Status D]]
 +
[[Category:Array Library]]
 +
<!-- For Lumina use, do not change -->
 +
 
 +
== CopyIndex(i'', omitNull'') ==
 +
Makes a copy of the values in «i» and returns these as a list which can be assigned to a new index variable, global or local.  «i» must be a list or 1-D array.  In the case of a 1-D array, the values in the array itself are returned, essentially removing the index from the array (converting the array to a list, or equivalently, to an array with an implicit dimension).  When the optional «omitNull» is true, then [[Null]] values are removed («omitNull» is new to [[Analytica 5.0]]).
 +
 
 +
[[CopyIndex]] is most often used to make a copy of an existing index for the purpose of defining a new index with the same index values.  For example, if you need to represent a square matrix, you will need two indexes with the same length -- defining the second using [[CopyIndex]] allows the second index to automatically adapt if the first index changes. 
 +
 
 +
When defining a local index using [[Index..Do]], a [[CopyIndex]] is implicitly performed, so an explicit call to [[CopyIndex]] is unnecessary (unless you want to remove Nulls).
 +
 
 +
== Examples ==
 +
Suppose you want to create a matrix of distances between a set of origins and destinations, which are each the same set of cities:
 +
 
 +
:<code>Index Origins</code>
 +
:'''Definition:''' <code>['London', 'New York', 'Tokyo', 'Paris', 'Delhi', 'McMurdo']</code>
 +
 
 +
:<code>Index Destinations</code>
 +
:'''Definition:''' <code>[[CopyIndex]](Origins)</code>
 +
:<code>Variable Flight_times := [[Table]](Origins, Destinations)</code>
 +
 
 +
If you were to define <code>Destinations</code> simply as <code>Origins</code>, without using [[CopyIndex]](), <code>Destinations</code> would be indexed by <code>Origins</code>, and the resulting table would have only one dimension index.  By defining <code>Destinations</code> with [[CopyIndex]](), it becomes a separate index, so that the table has two dimensions.
 +
 
 +
:<code>Variable Primary_Airport</code>
 +
:'''Definition:''' <code>[[Table]](Destinations)('LHR', 'JFK', 'HND', 'CDC', 'DEL', [[Null]])</code>
 +
 
 +
<code>Index AirportCodes</code>
 +
:'''Definition:''' <code>[[CopyIndex]](Primary_Airport, omitNull:True) &rarr; ['LHR', 'JFK', 'HND', 'CDC', 'DEL']</code>
 +
 
 +
but
 +
:<code>[[CopyIndex]](Primary_Airport) &rarr; ['LHR', 'JFK', 'HND', 'CDC', 'DEL', [[Null]]]</code>
 +
 
 +
== History ==
 +
 
 +
The «omitNull» parameter was introduced in [[Analytica 5.0]].
 +
 
 +
== See Also ==
 +
* [[Arrays and Indexes]]
 +
* [[Local Indexes]]
 +
* [[Index..Do]]
 +
* [[Subset]]
 +
* [[Array]]
 +
* [[Set Functions]] like [[SetDifference]] and [[Unique]], which can be used to remove duplicates.
 +
* [[To edit a table]] -- copying a table

Latest revision as of 01:19, 28 April 2016


CopyIndex(i, omitNull)

Makes a copy of the values in «i» and returns these as a list which can be assigned to a new index variable, global or local. «i» must be a list or 1-D array. In the case of a 1-D array, the values in the array itself are returned, essentially removing the index from the array (converting the array to a list, or equivalently, to an array with an implicit dimension). When the optional «omitNull» is true, then Null values are removed («omitNull» is new to Analytica 5.0).

CopyIndex is most often used to make a copy of an existing index for the purpose of defining a new index with the same index values. For example, if you need to represent a square matrix, you will need two indexes with the same length -- defining the second using CopyIndex allows the second index to automatically adapt if the first index changes.

When defining a local index using Index..Do, a CopyIndex is implicitly performed, so an explicit call to CopyIndex is unnecessary (unless you want to remove Nulls).

Examples

Suppose you want to create a matrix of distances between a set of origins and destinations, which are each the same set of cities:

Index Origins
Definition: ['London', 'New York', 'Tokyo', 'Paris', 'Delhi', 'McMurdo']
Index Destinations
Definition: CopyIndex(Origins)
Variable Flight_times := Table(Origins, Destinations)

If you were to define Destinations simply as Origins, without using CopyIndex(), Destinations would be indexed by Origins, and the resulting table would have only one dimension index. By defining Destinations with CopyIndex(), it becomes a separate index, so that the table has two dimensions.

Variable Primary_Airport
Definition: Table(Destinations)('LHR', 'JFK', 'HND', 'CDC', 'DEL', Null)

Index AirportCodes

Definition: CopyIndex(Primary_Airport, omitNull:True) → ['LHR', 'JFK', 'HND', 'CDC', 'DEL']

but

CopyIndex(Primary_Airport) → ['LHR', 'JFK', 'HND', 'CDC', 'DEL', Null]

History

The «omitNull» parameter was introduced in Analytica 5.0.

See Also

Comments


You are not allowed to post comments.