In other words, I want to do something like
A[[-1, 0, 1], [2, 3, 4]] += np.ones((3, 3))
instead of
A[-1:3, 2:5] += np.ones((1, 3))
A[0:2, 2:5] += np.ones((2, 3))
Slicing a One-dimensional Array Index '3' represents the starting element of the slice and it's inclusive.
NumPy Arrays are faster than Python Lists because of the following reasons: An array is a collection of homogeneous data-types that are stored in contiguous memory locations. On the other hand, a list in Python is a collection of heterogeneous data types stored in non-contiguous memory locations.
argwhere() function is used to find the indices of array elements that are non-zero, grouped by element. Parameters : arr : [array_like] Input array. Return : [ndarray] Indices of elements that are non-zero.
There are two main reasons why we would use NumPy array instead of lists in Python. These reasons are: Less memory usage: The Python NumPy array consumes less memory than lists. Less execution time: The NumPy array is pretty fast in terms of execution, as compared to lists in Python.
If I understand correctly, you can do what you want to do with the following:
A[[[-1],[0],[1]],[2,3,4]] += np.ones((3, 3))
However, the numpy folks made a function, ix_, to make it a little bit easier:
A[np.ix_([-1,0,1],[2,3,4])] += np.ones((3, 3))
I hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With