3

I need to replace multiple values of a 3d NumPy array with other values using the list of indexes. For example:

old_array = np.full((224,224,3),10)
# in below list first position element are indexes and 2nd one are their corresponding values
list_idx_val = [[array([[ 2, 14,  0],
   [ 2, 14,  1],
   [ 2, 14,  2],
   [99, 59,  1],
   [99, 61,  1],
   [99, 61,  2]], dtype=uint8), array([175, 168, 166,119, 117, 119], dtype=uint8)]
#need to do
old_array[2,14,1] =168

One way is to simply access index values and replace old values with the new one. But the NumPy array and list of indexes is quite big. I would be highly thankful for a fast and efficient solution (without a loop, preferably slicing or other maybe) to replace the array values as I need to create thousands of arrays by replacing values using such list of indexes with minimal latency.

1
  • 1
    Shouldn't old_array[2,14,1] become 168? Commented Oct 24, 2020 at 4:57

1 Answer 1

1

Let's do array indexing:

idx, vals = list_idx_val

old_array[idx[:,0], idx[:,1], idx[:,2]] = vals

Output (plt.show):

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.