What i need is a way to get "fancy indexing" (y = x[[0, 5, 21]]) to return a view instead of a copy.
I have an array, but i want to be able to work with a subset of this array (specified by a list of indices) in such a way that the changes in this subset is also put into the right places in the large array. If i just want to do something with the first 10 elements, i can just use regular slicing y = x[0:10]. That works great, because regular slicing returns a view. The problem is if i don't want 0:10, but an arbitrary set of indices.
Is there a way to do this?
I don't think there is a way around this. My understanding is that 'fancy indexing' will always return a copy. The best solution I can think of is to manipulate y
and then use the same fancy indexes to change the values of x
afterwards:
ii = [0, 5, 21]
y = x[ii]
<manipulate y>
x[ii] = y
You can just do:
y = x[[0,1,4]]
func(y)
x[[0,1,4]] = y
I don't think you can get views with fancy indexing. You might not want to, as I think fancy indexing is pretty slow, it should be faster to just copy the data once.
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