# Subscript and slice of a subarray

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

## Overview

These constructs and functions let you select a slice or subarray out of an array.

## x[i = v]: Subscript construct

This is the most common method to extract a subarray:

x[i = v]

It returns the subarray of «x» for which index «i» has value «v». If «v» is not a value of index «i», it returns Null, and usually gives a warning.

If «x» does not have «i» as a index, it just returns «x». The reason is that if an array «x» is not indexed by «i», it means «x» is constant over all values of «i».

You can apply the subscript construct to an expression, simply by putting the square bracket immediately after the expression:

(Revenue - Cost)[Time = 2010]

Indexing by name not position: You can subscript over multiple dimensions, for example:

x[i = v, j = u]

The ordering of the indexes is arbitrary, so you get the same result from:

x[j = u, i = v]

Indexing by name means that you don’t have to remember or use any intrinsic ordering of indexes in an array, such as rows or columns, inner or outer, common to most computer languages.

The value «v» can be an array with some index other than «i» of values from the index «i». For example, «v» might be a subset of «i». In that case, the result is an array with the index(es) of «v» containing the corresponding elements of «x».

## Subscript(x, i, v)

The Subscript function is identical to the subscript construct x[i = v], using different syntax.

## x[@i = n]: Slice construct

The slice construct has an @ sign before the index. It is different from the subscript construct in that it refers to the numerical position rather than associating the value of index «i». It returns the «n»'th slice of «x» over index «i»:

x[@i = n]

The number «n» should be an integer between 1 (for the first element of index «i») and Size(i) for the last element of «i». If «n» is not an integer in this range, it returns Null, and returns a warning (unless warnings have been turned off).

Like the subscript construct, it can slice over multiple indexes, for example:

x[@i = n, @j = m]

And also like the subscript construct, the ordering of the indexes is arbitrary.

Mixing subscript and slice constructs: You can mix slice and subscript operations in the same expression in any order:

x[@i = 1, j = 2, k = 3]

## Slice(x, i, v)

The Slice function is identical to the slice construct x[@i=v], using different syntax.

## Slice(x, n)

If Slice() has only two parameters, and «x» has a single dimension, it returns the «n»'th element of «x». For example:

Index Quarters := 'Q' & 1..4
Slice(Quarters, 2) → 'Q2'

This method is the only way to extract an element from an unindexed array, for example:

Slice(2000..2003, 4) → 2003

It also works to get the «n»'th slice of a multidimensional array over an unindexed dimension, for example:

Slice(Quarters & ' ' & 2000..2003, 4) → Array(Quarters, ['Q1 2003', 'Q2 2003', 'Q3 2003', 'Q4 2003'])
Tip
If «x» is a scalar, or if «x» is an array with two or more indexed dimensions and no unindexed dimensions, Slice(x, n) simply returns «x».

Array

### Examples

Here, Analytica returns the values in Cost corresponding to the first element in Car_type, that is, the values of VW:

Slice(Cost, Car_type, 1) →
Mpg ▶
26 30 35
2185 1705 1585

Here, «n» is an array of positions:

Slice(Cost, Car_type, [1, 2]) →
Mpg ▶
26 30 35
1 2185 1705 1585
2 2810 2330 2210

## Preceding time slice: x[Time - 1]

x[Time - n] refers to the built-in index Time. It returns the value of variable «x» for the time period that is «n» periods prior to the current time period. This function is only valid inside the Dynamic() function

## @: Index Position Operator

The position of value «x» in an index «i» is the integer «n» where «x» is the «n»'th element of «i». «n» is a number between 1 and Size(i). The first element of «i» is at position 1; the last element of «i» is at position Size(i). The position operator @ offers three ways to work with positions:

• @i → an array of integers from 1 to Size(i) indexed by «i».
• @[i = x] → the position of value «x» in index «i» or 0 if «x» is not an element of «i».
• e[@i = n] → the «n»'th slice of the value of expression «e» over index «i».

### Examples

Index Car_type :=
 VW Honda BMW
@Car_type →
Car_type ▶
VW Honda BMW
1 2 3
@[Car_type='Honda'] → 2
Car_type[@Car_type=2] → 'Honda'

### More examples and tips

Index Time:
 0 1 2 3 4
Years := Time + 2007 →:
 2007 2008 2009 2010 2011
@Time →
Time  ▶
0 1 2 3 4
1 2 3 4 5
@[Time = 2] → 3
@Time = 3 →
Time  ▶
0 1 2 3 4
0 0 1 0 0
Time[@Time = 3] → 2
(Time + 2007)[@Time = 3] → 2009
Tip
You can use this operator to re-index an array by another index having the same length but different elements. For example, suppose Revenue is indexed by Time, this following gives the same array but indexed by Years: Revenue[@Time = @Years]

## Choice(i, n, inclAll, eval, result, separator)

Appears as a popup menu in the definition field, allowing selection of the «n»'th item from «i». Choice() must appear at the topmost level of a definition or table cell. It cannot be used inside another expression. The optional «inclAll» parameter controls whether the "All" option («n» = 0) appears on the popup («inclAll» defaults to True). When you select All, Analytica runs the model on all inputs so you can see how your result varies as this parameter varies, a technique commonly referred to as parametric analysis. If you don’t want All to be an option, specify «inclAll» as false.

When using Choice in a table cell, it is a good practice to set «inclAll» to false.

In a choice definition (non-table), you can specify «i» to be Self if the Domain attribute contains an explicit list. Otherwise, «i» is an index that contains the list of options. «n» is the currently selected item, with 1 being the first item, or it is 0 for the All option. When the user makes a new selection, Analytica will rewrite the definition by changing «n» to a new number. «n» must be a literal number, it cannot be a general expression.

When index «i» contains handles to objects, the optional boolean «eval» parameter controls whether the handle is returned (eval: false) or the result of evaluating the variable is returned (eval: true). The optional «result» parameter can be an array indexed by «i», which specifies the value returned and which may be different from the elements of «i». You can include separators in the choice menu by specifying a value for the optional «separator» parameter. The elements of «i» matching «separator» will appear as non-selectable separators in the popup menu.

### Examples

Choice(Years, 2) → 1986

If «n» = 0, and «inclAll» is true, it returns all values of «i»:

Choice(Years, 0, 1) →
Years ▶
1985 1986 1987 1988