filmov
tv
Set numpy array values past a certain index to 0 using another array

Показать описание
Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!Set numpy array values past a certain index to 0 using another array
I have two arrays, one of floats and one of integers
[0.3, 1.8, 4.2]])
[0.3, 1.8, 4.2]])
I need to amend arr1 such that
arr1 [0, arr2[0]:] = 0
arr1 [1, arr2[1]:] = 0
arr1 [0, arr2[0]:] = 0
arr1 [1, arr2[1]:] = 0
obtaining
array([[1.5 , 0.75, 0. ],
[0.3 , 0. , 0. ]])
array([[1.5 , 0.75, 0. ],
[0.3 , 0. , 0. ]])
i.e. the nth element of arr2 dictates the number of non-zero elements in row n of arr1.
arr2
arr1
How can I vectorise this for large arrays, and extend this to further dimensions? Say
how can I change arr1 (or create a new arr3 depending on implementation) such that every slice
arr1
arr3
arr1[:, :, i], arr2[:, i]
arr1[:, :, i], arr2[:, i]
follows the above?
The final implementation will be on the order of 100,000 x 100 x 10,000.
Tags: python,arrays,numpySource of the question:
Question and source license information: