power2
power2

Reputation: 999

Numpy set range where less than

def updatemap(depthmap, p1, p2, value):
    maps = depthmap[0:580,p1[0]:p2[0]]
    maps[maps < value] = value
    depthmap[0:580,p1[0]:p2[0]] = maps

This is the current way I do it. But it requires I make a copy of the range, then set the range where the value is less than, then copy it back. The copying would make it slow. Is there any syntax I can use?

Upvotes: 0

Views: 40

Answers (1)

user2357112
user2357112

Reputation: 281528

Assuming depthmap is a NumPy array, this part:

maps = depthmap[0:580,p1[0]:p2[0]]

doesn't actually make a copy. Unlike with lists and tuples, NumPy slicing creates a view of the original array. Thus, the next line:

maps[maps < value] = value

modifies the original depthmap array, and the line after that:

depthmap[0:580,p1[0]:p2[0]] = maps

is superfluous. You can just remove it:

def updatemap(depthmap, p1, p2, value):
    maps = depthmap[0:580,p1[0]:p2[0]]
    maps[maps < value] = value

Upvotes: 2

Related Questions