Functions that create indexes
Using a function to create an index
It is usually easiest to define an index as a list, list of labels, or sequence. Sometimes, you need to define an index using a more general expression, as a list of expressions, a list of variables, or a function such as Subset(), Concat(), and SortIndex(). This section describes these and other functions that you can use to create indexes.
[ u1, u2, u3, … um ]
A simple way to define an index is specify its definition as a list of values separated by commas and surrounded by square brackets. The values can be numbers, text values, or other expressions.
Examples
[8000, 12K, 15K]
['VW', 'Honda', 'BMW']
These lists are equivalent to using the List or List of Labels options in the expr menu.
List of variables
A list of variables contains identifiers of variables in square brackets, separated by commas. Usually, the simplest way to create a list of variables is to define the variable initially as an empty list, for example:
Variable CompareVars := []
When you draw an arrow from a variable, A
, into CompareVars
, it will automatically add A
as the next item in the list:
CompareVars := [A]
Suppose you draw arrows from B
and C
, the definition will become:
CompareVars := [A, B, C]
When you draw an arrow from a variable already in the list, it removes it from the list. Suppose we draw an arrow from B
to CompareVars
, it will become:
CompareVars := [A, C]
The result of CompareVars
is an array of the values of the variables it contains, with a self index, also called CompareVars
, that usually shows the titles of the variables.
If any or all the variables contain arrays, the result contains the union of the indexes of the contained variables. For example if A is an atom (not an array) and C is indexed by c, the result will be indexed by I. The slice of CompareVars
for A will have the same value of A repeated for each value of A.
Self index: The result will contain an extra index, a self index of CompareVars
, comprising the list of the variables.
Clickable titles or identifiers in table: Usually these display the titles of the variables in a table or graph result. (If you select Show by Identifier from the Object menu (or press Control+y) it toggles to show the identifiers instead of titles. If you double-click a title (or identifier) in a table, it will open the Object window for that variable. The values in the self index are actually handles to the variables.
m .. n
Returns a sequence of successive integers from m to n — increasing if n < m, or decreasing if n> m. For example:
2003..2006 → [2003, 2004, 2005, 2006]
5 .. 1 → [5, 4, 3, 2, 1]
It is equivalent to Sequence(m, n).
Sequence(start, end, step): Creates a list of numbers from start to end by increments of step (defaults to 1), e.g. Sequence(1, 5) → [1, 2, 3, 4, 5]
.
Concat(i, j): Concatenates two arrays or lists (1-D arrays) to create a new array or list with a longer index, e.g. if Index Year1 := 2006 .. 2008
, Index Years2 := 2009 .. 2010
and Index YearsAll := Concat(Years1, Years2)
then YearsAll → [2006, 2007, 2008, 2009, 2010]
,
Subset(d): Returns a list containing all the elements of d’s index for which d’s values are true, e.g. if :Index Years := 2005 .. 2009
then Subset(Years < 2008) → [2005, 2006, 2007]
.
CopyIndex(i): Makes a copy of the values of index i so that they can be assigned to a new / separate index variable , e.g. Index Origins := ['London', 'New York', 'Tokyo', 'Paris', 'Delhi'], Index Destinations := CopyIndex(Origins)
and Variable Flight_times := Table(Origins, Destinations)
Sortindex(d, i):
Assuming d is an array indexed by i, SortIndex() returns the elements of index i, reordered so that the corresponding values in d would go from smallest to largest value. The result is indexed by i. If d is indexed by dimensions other than i, each “column” is individually sorted, with the resulting sort order being indexed by the extra dimensions. To obtain the sorted array d, use this:
d[i=Sortindex(d, i)]
When d is a one-dimensional array, the index parameter i is optional. When omitted, the result is an unindexed list. Use the one-parameter form only when you want an unindexed result, for example to define an index variable. The one-parameter form does array abstract when a new dimension is added to d.
Examples
Unique(a, i)
Returns a maximal subset of i such that each indicated slice of a along i is unique.
The optional parameter position:true returns the positions of element in i, rather than the elements themselves. Specifying caseInsensitive:true ignores differences in upper and lower case in text values when determining if values are unique.
When to use
Use Unique() to remove duplicate slices from an array, or to identify a single member of each equivalence class.
Library
Array
DataSet →
Field ▶ | |||
---|---|---|---|
PersonNum ▼ | LastName | FirstName | Company |
1 | Smith | Bob | Acme |
2 | Jones | John | Acme |
3 | Johnson | Bob | Floorworks |
4 | Smith | Bob | Acme |
Unique(DataSet, PersonNum) → [1, 2, 3]
Unique(DataSet[Field='Company'], PersonNum) → [1, 3]
Enable comment auto-refresher