0

New to coding and to Python. I have this code which for some reason will only go through my first if-statement. I've looked at it for hours and I honestly can't tell why?

for i in range(1, trex.shape[0]-1): 
    for j in range(1,trex.shape[1]-1): 

        if shape[i,j] == 0:    # for 0 degree angles # ONLY GOES THROUGH THIS
            if trex[i,j] < trex[i, j+1] and trex[i,j] < trex[i,j-1]: 
                trex[i,j] = 0

        elif shape[i,j] == 45:    # for 45 degree angles # Never comes down here
            if trex[i,j] < trex[i-1, j+1] and trex[i,j] < trex[i+1,j-1]:
                trex[i,j] = 0

        else    # for 90 degree angles         # Or here
            if trex[i,j] < trex[i-1, j] and trex[i,j] < trex[i+1,j]:
                trex[i,j] = 0

So basically I want it to run through a 2d array. I have another 2d array called shape (same shape as trex) which has angles 0, 45, 90. First I look at the angle at the point [i,j] and then depending on which angle it has, I want it to look at the value of trex at the neighbours in the point. If the neighbouring points are BOTH lower than the middle point then I do nothing. Else I set the middle point ([i,j]) equal to 0. But when I run my code it only sets those with the angle 0 to 0. So it never goes down to my elif or else statement. I have no clue why :'( Hope it makes sense and any help is greatly appreciated!

J.Doe
  • 3
  • 1
  • How do you detect that the second and third branches are never taken? FYR: `if` is not a loop. – DYZ Jan 27 '18 at 16:20
  • I tried setting line 10 (2 lines below elif statement) to a value of 100 (which is higher than any value in trex and then examine trex afterwards. It didn't include this value! Same goes for the else statement! – J.Doe Jan 27 '18 at 16:27
  • You could try to put some debug printouts at several places or use a debugger. This way you'll operate on the real data and it will take much less time to resolve the problem. – ababak Jan 27 '18 at 16:32
  • How would I do that? Add print(trex[i,j]) somewhere? – J.Doe Jan 27 '18 at 16:44
  • As the first thing in your inner loop, print `i`, `j` and `shape[i,j]`. – John Gordon Jan 27 '18 at 17:01
  • Please provide a minimal working example of your arrays. Like 10 items per 1d-subarray could be enough. – Darkonaut Jan 27 '18 at 17:31
  • tried with `trex = np.array([[4,4,4],[1,1,1],[4,4,4]]) shape = np.array([[0,0,0],[0,45,0],[0,0,0]])` and it works: `print(trex)` outputs `[[4 4 4] [1 0 1] [4 4 4]]` – mugiseyebrows Jan 27 '18 at 17:53

0 Answers0