Indexing

Indexing of vectors

% nap "temperature = {20 21 20.6}"; # temperatures at time 1000, 1200, 1400
::NAP::134-134
% nap "time = {10 12 14}"
::NAP::136-136
% $temperature set coo time
% $temperature a
::NAP::134-134  f64  MissingValue: NaN  References: 1
Dimension 0   Size: 3      Name: time      Coordinate-variable: ::NAP::136-136
Value:
20 21 20.6
% [nap "temperature(0)"]
20
% [nap "temperature(1)"]
21
% [nap "temperature(2)"]
20.6
% [nap "temperature(3)"]
20
% [nap "temperature(-1)"]
20.6
% [nap "temperature(-2)"]
21
% [nap "temperature(0.5)"]
20.5
% [nap "temperature(1.5)"]
20.8
% [nap "temperature({1 0 2 -1})"]
21 20 20.6 20.6
% [nap "temperature{1 0 2 -1}"]
21 20 20.6 20.6
% [nap "temperature(0 .. 2 ... 0.5)"]
20 20.5 21 20.8 20.6

Inverse indexing of vectors (position of specified value)

% [nap "{2 5 7 2 5} @@@ {7 2 4 5}"]; # @@@ is 'match' (1st to match exactly)
2 0 _ 1
% $time
10 12 14
% [nap "time @ 14"]; # Find position (index) of value 14 in time
2
% [nap "time @ 11"]; # @ is 'interpolate'
0.5
% [nap "time @@ 13.1"]; # @@ is 'nearest'
2
% [nap "time @ {14 11 10 11.5}"]
2 0.5 0 0.75

Indirect indexing (via coordinate variable)

% [nap "temperature(time @ 14)"]
20.6
% [nap "temperature(     @ 14)"]; # Left operand of @ defaults to coordinate variable
20.6
% [nap "temperature(@ 11)"]
20.5
% [nap "temperature(time @@ 13.1)"]
20.6
% [nap "temperature(@@ 13.1)"]
20.6
% [nap "temperature(time@{14 11 10 11.5})"]
20.6 20.5 20 20.75
% [nap "temperature(@{14 11 10 11.5})"]
20.6 20.5 20 20.75

Shape-preserving indexing

% [nap "temperature(0.5)"] a; # (a = all) Both index & result are scalars
::NAP::51955-9298  f32  MissingValue: NaN  References: 0
Value:
20.5
% [nap "temperature({0.5 0 -1})"] a; # Both index & result are 3-element vectors
::NAP::51961-9977  f32  MissingValue: NaN  References: 0
Dimension 0   Size: 3      Name: (NULL)    Coordinate-variable: (NULL)
Value:
20.5 20 20.6
% [nap "temperature({{0 0.5 -1}{1 0 3}})"] a; # Both index & result are 2*3 matrices
::NAP::51967-9298  f32  MissingValue: NaN  References: 0
Dimension 0   Size: 2      Name: (NULL)    Coordinate-variable: (NULL)
Dimension 1   Size: 3      Name: (NULL)    Coordinate-variable: (NULL)
Value:
20.0 20.5 20.6
21.0 20.0 20.0

Indexing of Matrices

% # Define matrix m with coordinate variables
% nap "m = {
{6.1 1 -2 9}
{2#3 0 1.2}
}"
::NAP::46-46
% $m set coo "{10 20}" "50 .. 80 ... 10"
% $m a
::NAP::46-46  f64  MissingValue: NaN  References: 1  Unit: (NULL)
Dimension 0   Size: 2      Name: (NULL)    Coordinate-variable: ::NAP::48-48
Dimension 1   Size: 4      Name: (NULL)    Coordinate-variable: ::NAP::56-56
Value:
 6.1  1.0 -2.0  9.0
 3.0  3.0  0.0  1.2
% [nap "coordinate_variable(m, 0)"] a
::NAP::48-48  i32  MissingValue: -2147483648  References: 1  Unit: (NULL)
Dimension 0   Size: 2      Name: (NULL)    Coordinate-variable: (NULL)
Value:
10 20
% [nap "cv(m, 1)"] a; # can abbreviate 'coordinate_variable' to 'cv'
::NAP::56-56  i32  MissingValue: -2147483648  References: 1  Unit: (NULL)
Dimension 0   Size: 4      Name: (NULL)    Coordinate-variable: (NULL)
Value:
50 60 70 80

Cross-product indexing to extract single element (scalar)

% [nap m(0,0)]
6.1
% [nap m(1,2)]
0
% [nap m(1,-1)]
1.2
% [nap "m(@20, @70)"]; # Indirect indexing via coordinate variables
0
% [nap "m(@20, @75)"]; # Interpolate -- 0.5 * (0 + 1.2)
0.6
% [nap "m(0, @75)"];   # Interpolate -- 0.5 * (-2 + 9)
3.5
% [nap "m(@15, @75)"]; # Interpolate -- 0.5 * (0.6 + 3.5)
2.05
% [nap "m(@@16, @@74)"]; # Nearest
0

Cross-product indexing to extract multiple elements

% [nap "m(0, {2 0 -1})"]
-2 6.1 9
% [nap "m({1 0}, {2 0 -1})"]
 0.0  3.0  1.2
-2.0  6.1  9.0
% [nap "m(2, 0 .. 2)"]
6.1 1 -2
% [nap "m(1,)"];
3 3 0 1.2
% [nap "m(1, -)"]; # niladic '-' means 'reverse'
1.2 0 3 3
% [nap "m(-, -)"];
 1.2  0.0  3.0  3.0
 9.0 -2.0  1.0  6.1

Full indexing to extract single element

% [nap "m({0 0})"]
6.1
% [nap "m({1 2})"]
0
% [nap "m{1 -1}"]; # parentheses redundant
1.2

Full indexing to form a new array from randomly selected elements of an existing array

% [nap "m({{0 0}{1 1}})"]; # diagonal elements
6.1 3
% [nap "m{{0 0}{1 2}{1 -1}}"]
6.1 0 1.2
% [nap "m({
{{1 0}{2 1}{0 0}}
{{0 0}{1 3}{0 3}}
})"]
3.0 1.0 6.1
6.1 1.2 9.0

Author: Harvey Davies       © 2005, CSIRO Australia.       Legal Notice and Disclaimer
CVS Version Details: $Id: demo_indexing.html,v 1.2 2005/03/15 00:33:05 dav480 Exp $