- Array slicing
In
computer programming , array slicing is an operation that extracts certain elements from anarray and packages them as another array, possibly with different number of indices (ordimensions ) and different index ranges. Two common examples are extracting a substring from a string of characters (e.g. "ell" from "h"ell"o"), and extracting a row (or a column) of a rectangular matrix to be used as a vector.Depending on the
programming language and context, the elements of the new array may be aliased to (i.e., share memory with) those of the original array.Details
For "one-dimensional" (single-indexed) arrays — vectors, sequence, strings etc. — the most common slicing operation is extraction of zero or more consecutive elements. Thus, if we have a vector containing elements (2, 5, 7, 3, 8, 6, 4, 1), and we want to create an array slice from the 3rd to the 6th items, we get (7, 3, 8, 6). In
programming language s that use a 0-based indexing scheme, the slice would be from index "2" to "5".Reducing the range of any index to a single value effectively eliminates that index. This feature can be used, for example, to extract one-dimensional slices (vectors) or two-dimensional slices (rectangular matrices) from a three-dimensional array. However, since the range can be specified at run-time, type-checked languages may require an explicit (compile-time) notation to actually eliminate the trivial indices.
Array slicing is implemented by referencing every array through a
dope vector (or "descriptor"), a record that contains the address of the first array element, and then the range of each index and the corresponding coefficient in the element indexing formula. This technique also allows instant array transposition, flipping (index reversal), subsampling, etc.. For languages like C, where the indices always start at zero, the dope vector of an array with "d" indices has at least 1 + 2"d" parameters. For languages which allow arbitrary lower bounds for indices, like Pascal, the dope vector needs 1 + 3"d" entries.Some programming languages, which do not support true negative indices (as for example Ada and Pascal do) might use a special meaning for using negative indices to indicate the bounds of the slice, such that an offset from the end of the list can be used. In 0-based schemes, -1 generally would indicate the second-to-last item, while in a 1-based system, it would mean the very last item.
History
The concept of slicing was surely known even before the invention of
compiler s. Slicing as a language feature probably started withFORTRAN (1957), more as a consequence of non-existent type and range checking than by design. The concept was also alluded to in the prelimiary report for the IAL (ALGOL 58) in that the syntax allowed one or more indices of an array element (or, for that matter, of a procedure call) to be omitted when used as an actual parameter.Kenneth Iverson 's APL (1957) had very flexible multi-dimensional array slicing, which contributed much to the languages' expressive power and popularity.ALGOL 68 (1968) introduced comprehensive multi-dimension array slicing and trimming features.Array slicing facilities have been incorporated in several modern languages, such as Ada 2005, Boo, D, Fortran 90, Matlab,
Perl , Python, S-Lang,Windows PowerShell and the mathematical/statistical languages GNU Octave, S and R.Timeline of slicing in various programming languages
1966: Fortran 66
The Fortran 66 programmers were only able to take advantage of slicing matrices by row, and then only when passing that row to a
subroutine : SUBROUTINE PRINT V(VEC, LEN) REAL VEC(*) PRINT *, (VEC(I), I=1, LEN) END PROGRAM MAIN PARAMETER(LEN=3) REAL MATRIX(LEN,LEN) DATA MATRIX/1,1,1, 2,4,8, 3,9,27/ CALL PRINT V(MATRIX(1,2),LEN) ENDResult: 2. 4. 8.Note that there is no
dope vector in FORTRAN 66 hence the length of the slice must also be passed as an argument - or some other means - to theSUBROUTINE
. 1970s Pascal and C had similar restrictions.1968:
Algol 68 Algol68 final report contains an early example of slicing, slices are specified in the form: [lower bound:upper bound] ¢ for computers with extended character sets ¢or (LOWER BOUND..UPPER BOUND) # FOR COMPUTERS WITH ONLY 6 BIT CHARACTERS. #Both bounds are inclusive and can be omitted, in which case they default to the declared array bounds. Neither the stride facility, nor diagonal slice aliases are part of the revised report.
Examples: [3,3] real a:=((1,1,1),(2,4,8),(3,9,27)); # declaration of a variable matrix # [,] real c =((1,1,1),(2,4,8),(3,9,27)); # constant matrix, the size is implied # ref [] real row:=a [2,] ; # alias/ref to a row slice # ref [] real col2=a [,2] ; # permanent alias/ref to second column # print ((a [:,2] ,newline)); # second column slice # print ((a [1⌈a,:] ,newline)); # last row slice # print ((a [:,2⌈a] ,newline)); # last column slice # print ((a [:2,:2] ,newline)); # leading 2-by-2 submatrix "slice" #
+1.000010+0 +4.000010+0 +9.000010+0 +3.000010+0 +9.000010+0 +2.700010+1 +1.000010+0 +8.000010+0 +2.700010+1 +1.000010+0 +1.000010+0 +2.000010+0 +4.000010+0
1970s:
MATLAB /GNU Octave /Scilab > A = round(rand(3,4,5)*10) # 3x4x5 three-dimensional or cubic array > A(:,:,3) # 3x4 two-dimensional array along first and second dimensions ans = 8 3 5 7 8 9 1 4 4 4 2 5 > A(:,2:3,3) # 3x2 two-dimensional array along first and second dimensions ans = 3 5 9 1 4 2 > A(1,:,3) # single-dimension array along second dimension ans = 8 3 5 7 > A(1,2,3) # single value ans = 3
1976 : S / R
Arrays in S and GNU R are always one-based, thus the indices of a new slice will begin with "one" for each dimension, regardless of the previous indices. Dimensions with length of "one" will be dropped (unless drop=FALSE). Dimension names (where present) will be preserved.
> A <- array(1:60,dim=c(3,4,5)) # 3x4x5 three-dimensional or cubic array > A [,,3] # 3x4 two-dimensional array along first and second dimensions [,1] [,2] [,3] [,4] [1,] 25 28 31 34 [2,] 26 29 32 35 [3,] 27 30 33 36 > A [,2:3,3,drop=FALSE] # 3x2x1 cubic array subset (preserved dimensions) , , 1 [,1] [,2] [1,] 28 31 [2,] 29 32 [3,] 30 33 > A [,2,3] # single-dimension array along first dimension [1] 28 29 30 > A [1,2,3] # single value [1] 28
1977: Fortran 77
The Fortran 77 standard introduced the ability to slice and concatenate strings:
Produces: BCD
Such a strings could be passed by reference to another subroutine, the length would also be passed transparently to the subroutine as a kind of short dope vector.
Again produces: BCD
=1983: Ada 83 and above=Ada 83 supports slices for all array types. Like Fortran 77 such a arrays could be passed by reference to another subroutine, the length would also be passed transparently to the subroutine as a kind of short dope vector.
Produces: BCD
Note: Since in Ada indices are n-based the term
Text (2 .. 4)
will result in an Array with the base index of 2.The definition for
Text_IO.Put_Line
is:The definition for
String
is:As Ada supports true negative indices as in
type History_Data_Array is array (-6000 .. 2010) of History_Data;
it places no special meaning on negative indices. In the example above the termSome_History_Data (-30 .. 30)
would slice theHistory_Data
from 30 BC to 30 AD.1987:
Perl If we have as above, will represent the sliced array (7, 3, 8, 6).
1991: Python
If you have a list , then the first 3 elements, middle 3 elements, and last 3 elements would be:Note that Python allows negative list indices. The index -1 represents the last element, -2 the penultimate element, etc.Python also has more advanced slicing operators using the double colon (::) index operator. For example, the code
1992: Fortran 90 and above
In Fortran 90, slices are specified in the form
Both bounds are inclusive and can be omitted, in which case they default to the declaredarray bounds. Stride defaults to 1. Example:
=1998: S-Lang=Array slicing was introduced in version 1.0. Earlier versions did notsupport this feature.
Suppose that A is a 1-d array such as
Then an array B of first 5 elements of A may be created usingA = [1:50] ; % A= [1,2,3,...49,50]
Similarly, B may be assigned to an array of the last 5 elements of A via:B = A:4 ;
Other examples of 1-d slicing include:B = A-5: ;A [-1] % The last element of A A [*] % All elements of A A % All even elements of A A % All odd elements of A A % All even elements in the reversed order A [0:3] , [10:14] % Elements 0-3 and 10-14Slicing of higher dimensional arrays works similarly:
A [-1,*] % The last row of A A [1:5] , [2:7] % 2d array using rows 1-5 and columns 2-7 A [5:1:-1] , [2:7] % Same as above except the rows are reversedArray indices can also be arrays of integers. For example, supposethat
I= [0:9]
is an array of 10 integers. ThenA [I]
is equivalent to an array of the first 10 elementsofA
. A practical example of this is a sortingoperation such as:I = array_sort(A); % Obtain a list of sort indices B = A [I] ; % B is the sorted version of A C = A [array_sort(A)] ; % Same as above but more concise.1999: D
Consider the statically allocated array:
Take a slice out of it:
and the contents of
b []
will be[7, 3, 8]
. The first index of the slice is inclusive, the second is exclusive. D array slices are aliased to the original array, so:means that
a []
now has the contents[2, 5, 7, 3, 10, 6, 4, 1]
. To create a copy of the array data, instead of only an alias, do:
=2005: fish=Arrays in fish are always one-based, thus the indices of a new slice will begin with "one", regardless of the previous indices.
> set A (seq 3 2 11) # $A is an array with the values 3, 5, 7, 9, 11 > echo $A [(seq 2)] # Print the first two elements of $A 3 5 > set B $A [1 2] # $B contains the first and second element of $A, i.e. 3, 5 > set -e A [$B] ; echo $A # Erase the third and fifth elements of $A, print $A 3 5 9
2006:
Windows PowerShell Arrays are zero-based in PowerShell and can be defined using the comma operator: PS> $a = 2, 5, 7, 3, 8, 6, 4, 1
Print the first two elements of $a: PS> $a [0,1] # Print 2 and 5
Take a slice out of it using the range operator: PS> $b = $a [2..5] # Content of $b will be: 7, 3, 8, 6.
Get the last 3 elements: PS> $a [-3..-1] # Print 6, 4, 1
Return the content of the array in reverse order: PS> $a [($a.Length-1)..0] # Length is a property of System.Object []
Wikimedia Foundation. 2010.