Subset operations

From DavinciWiki
Jump to: navigation, search

Description

Individual elements of an array can be accessed using the range operator. Ranges are specified as a from and to value, separated by a colon. The range operator takes a range for each axis, seperated by commas, and applies them to the specified array, to produce a subset. A range value of 0 for either the from or to value indicates the maximum range of the data in that axis.

Every numeric value in daVinci is actually a 3 dimensional array of values. The constants that have been shown previously are 3-D arrays, with every axis having a dimension of 1.

In addition to the subsetting of arrays arrays of strings and strings can also be subset.

Below are some procedures (both common and uncommon) and uses of subsetting arrays and strings.

Procedure

Arrays

This is the initial step where the array that will be worked with for the remainder of the examples is created.

dv> a=create(5,5,1)
5x5x1 array of int, bsq format [100 bytes]
0       1       2       3       4
5       6       7       8       9
10      11      12      13      14
15      16      17      18      19
20      21      22      23      24

The subset of the array is taken from position 2:3 in both the x and y directions with all of the z values specified. These operations can be done in the z-direction as well, but for simplcity of the example a 2-dimensional case is shown.

dv> a[2:3,2:3,]
2x2x1 array of int, bsq format [16 bytes]
6       7
11      12

It is also possible to set a range of values to one value or to perform any desired operation to this range.

dv> a[2:3,2:3,]=23
2x2x1 array of int, bsq format [16 bytes]
23      23
23      23

dv> a[2:3,2:3,]*2
2x2x1 array of int, bsq format [16 bytes]
12      14
22      24

It is also possible to give the range operator an open ended colon which includes either from the start of the array to the specified location or to the end of the array from the specified location. This is best expressed in 2 examples.

This selects all of the elements in the x direction from the start of the array until the 3rd position.

dv> a[:3,2:3]
3x2x1 array of int, bsq format [24 bytes]
5       6       7
10      11      12

This selects all of the elements in the x direction from the third position to the end of the array.

dv> a[3:,2:3]
3x2x1 array of int, bsq format [24 bytes]
7       8       9
12      13      14

It is also possible to not specify a range for an axis which causes the range operator to select the entire range.

dv> a[,2:3]
5x2x1 array of int, bsq format [40 bytes]
5       6       7       8       9
10      11      12      13      14

It is also possible to select single values by simply specifying the exact coordinates of the desired pixel.

dv> a[3,3]
12

Finally it is also possible to select every nth pixel using this operator. This can be done on any axis and is completed by adding another colon which is the interval to select on. By default this value is 1 which yields all of the results above. For this example the entire range will be selected with every 2nd pixel.

dv> a[::2,::2]
3x3x1 array of int, bsq format [36 bytes]
0       2       4
10      12      14
20      22      24

This can also be done with a subset of the original data.

dv> a[2:4:2,2:4:2]
2x2x1 array of int, bsq format [16 bytes]
6       8
16      18


Strings and Arrays of Strings

This is the initial step where the string that will be worked with for the remainder of the examples is created.

dv> b="I love davinci"
"I love davinci"
dv> b=cat(b,b,b,axis=y)
Text Buffer with 3 lines of text
   1: I love davinci
   2: I really love davinci
   3: I really really love davinci

The first and most simple example is selecting a string from an array of strings. You can see from the example below that this operation is exactly the same as the operation for an array.

dv> b[,2]
"I really love davinci"

A slightly more complicated case is selecting a range of strings from the array of strings.

dv> b[,2:]
Text Buffer with 2 lines of text
    1: I really love davinci
    2: I really really love davinci

Now we will select a range of characters from within a single string subset.

dv> b[3:10,2]
"really l"

The most complicated example is selecting a range of characters from within a subset of an array of strings.

dv> b[10:15,2:]
Text Buffer with 2 lines of text
    1: love d
    2: really


DavinciWiki Mini-Nav Bar

Contents


Contact Developers

  • davinci-dev [AT] mars.asu.edu

All other topics

  • See navigation on the left

Functions Used

Personal tools