0

I am trying to refactor procedural code of a numpy 3d array into it's vectorized equivalent and the results are not matching .

import numpy as np 
              
arr = np.empty((16,73,144))

boolArray = np.zeros((14,73,144)) 

for k in range(0,16): 
   for j in range(0,73): 
      for i in range(0,144): 
        if (k == 0):
            doSomething():
        else if (k == 15):
             doSomething():
        else:
         if (arr[k+1,j,i] == arr[k-1,j,i]): 
              boolArray[k,j,i] = True

The vectorized counterpart -

# isArrEqual is a boolean array of identical shape to boolArray 
isArrEqual = (arr[2:,:,:] == arr[0:-2,:,:])

In my real data I get 6000 true entries for the procedural code and no trues at all in the vectorized counterpart.

Where I am going wrong ?

2
  • 1
    arr = np.empty(16,73,144) should be arr = np.empty((16,73,144)). Same for boolArray Commented Oct 21, 2021 at 7:01
  • @Corralien Edited. Thanks for pointing out the error. Commented Oct 21, 2021 at 7:02

1 Answer 1

1

You probably have a mistake in your procedural code. I believe it should be

for k in range(1, 15):  # changed 14 to 15 
   for j in range(0,73): 
      for i in range(0,144): 
         if (arr[k+1,j,i] == arr[k-1,j,i]): 
              boolArray[k-1,j,i] = True  # changed k to k-1

I don't see any issues in the numpy code.

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

6 Comments

Am I allowed to make an edit to the question based on your answer?
But doesn't that solve your problem?
No it does not. So can I make an edit ?
Yes. please be precise with data types in arrays
@gansub, you still have issues in your procedural code. (IndexError for k = 14 + syntax errors, + you didn't changed k to k - 1 in the last line)
|

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.