Home > AI > Uncategorized

access array elements

Example 1: 1d array

import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
# idx: [2, 5)
print(a[2:5])
# [3, 4, 5]

Example 2: 2d array (without changing dimensions)

import numpy as np
a = np.array([
[1, 2, 3],
[2, 3, 2],
[4, 5, 2]
])

start = [0, 1]
end = [1, 2]

print(a[start[0]:end[0], start[1]:end[1]])
Related posts:

Leave a Reply