Python Forum
How to avoid slow cursor operations?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to avoid slow cursor operations?
Hi. I'm using numpy as np. I have a 2D array called CC of all zeros and ones. For every one in CC, I want to change all adjacent array values to one. I want to do this recursively, and then I want to count how many steps it takes for the last zero to disappear. That is the point of the code: how far away from a one is the farthest zero. I initialized CC with
CC = np.zeros((N,N),dtype="int")  
and then I wrote some ones into it. My first idea was to make 8 copies of CC, each with the values shifted by one, (eight copies for up/down,left/right, and 4 diag) and then add those copies to CC, and then iterate. However, all the shift commands I found like roll do not shift the entire array but they send the end of each line to the beginning of the next. As it is, if CC is 1000 x 1000, then I have the below cursor doing ~eight operations for one million times. There must be a better way!
nn = 0
CCtmp = np.zeros((N,N),dtype="int") 
while nn < 1:
    for i in range(len(CC)-1):
        for j in range(len(CC)-1):
            if i**2+ j**2 > N**2:         ## Condition for the part of CC I don't care about
                CCtmp[i,j] = 1
            if (CC[i,j] == 1 ): 
                    CCtmp[i+1,j] = 1  ## For every black pixel, color adjacent black
                    CCtmp[i,j+1] = 1  
                    CCtmp[i+1,j+1] = 1   
                    CCtmp[i-1,j] = 1
                    CCtmp[i,j-1] = 1  
                    CCtmp[i-1,j-1] = 1   
                    CCtmp[i-1,j+1] = 1   
                    CCtmp[i+1,j-1] = 1   
    CC = CC + CCtmp
    nn = np.amin(CC)
    MAX_RADIUS += 1
print('Max radius within ' + str(N) + ' is less than ' + str(MAX_RADIUS) + '.\n\n') 
Keeping mind the point of the code: how far away from a one is the farthest zero, any other suggestions are welcome.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Replicate Excel operations with Python Lumberjack 3 1,391 May-10-2022, 01:44 AM
Last Post: Lumberjack
  Cursor Variable inside Another Cursor . CX_ORacle paulo79 1 1,100 Apr-09-2022, 10:24 AM
Last Post: ibreeden
  Program demonstrates operations of bitwise operators without using bitwise operations ShawnYang 2 1,503 Aug-18-2021, 03:06 PM
Last Post: deanhystad
  Random Choice Operations Souls99 6 2,497 Jul-31-2020, 10:37 PM
Last Post: Souls99
  Create bot to automate operations in IQ Option JonatasCavalini 0 9,861 Jul-19-2020, 02:23 AM
Last Post: JonatasCavalini
  Two operations in two ranges salwa17 3 1,823 Jun-22-2020, 04:15 PM
Last Post: perfringo
  Operations on indexed variables in loop Fibulavie 1 1,700 Aug-14-2019, 06:07 AM
Last Post: fishhook
  Confused by order of operations ward 4 2,306 Jan-22-2019, 08:53 PM
Last Post: Larz60+
  Best way to return Cursor Data rajuarien 0 1,781 Jul-29-2018, 09:47 PM
Last Post: rajuarien
  Beginner user: mathematical operations Mahdi1994 1 2,579 Mar-19-2018, 11:07 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020