Description
ndarray objects use a similar indexing system to iterable Python data types. To access an element of a multidimensional array, we use an "outside-in" approach. For instance, to access the location of the number 7 in the following 3 dimensional array a:
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
we would call the index of the 2-dimensional array containing [5 6] and [7 8], then the index of 7 in the 1-dimensional array containing 7 like so: a[1, 1, 0]
Sage Cell
Code
a = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(a[1, 1, 0])
Options
Slicing Arrays
We can take multiple items at once by passing a range of indices instead of only one using :. For instance, if we wanted all items in an array b from index 0 to index 5, we would use 0:6. We can also tell the program to simply get everything to the end of the array by leaving the range unbounded, like so: 0:. Note the last number in the given range is excluded. As an example, the following cell shows how to get the first item in every 1 -dimensional array stored in 3 dimensional array a.
Code
a = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(a[0:, 0:, 0])
Tags
Primary Tags:
Secondary Tags:
A list of possible tags can be found at The WeBWorK Open Problem Library. For linear algebra tags see the Curated Courses Project.
Related Cells
Any related cells go here. Provide a link to the page containing the information about the cell.
Attribute
Permalink:
Author:
Date: 06 Apr 2019 19:08
Submitted by: Zane Corbiere