I have a two-dimensional numpy array (uint16), how can I truncate all values โโabove a certain barrier (say 255) to this barrier? The remaining values โโshould remain unchanged. Using a nested loop seems inefficient and awkward.
import numpy as np my_array = np.array([[100, 200], [300, 400]],np.uint16) my_array[my_array > 255] = 255
the conclusion will be
array([[100, 200], [255, 255]], dtype=uint16)
actually there is a specific method for this, 'clip':
import numpy as np my_array = np.array([[100, 200], [300, 400]],np.uint16) my_array.clip(0,255) # clip(min, max)
exit:
In case your question was not related to bit depth, like JBernardo's answer, a more general way to do this would be something like this: (after editing, my answer is now almost the same as hiss)
def trunc_to (my_array, limit): too_high = my_array> limit my_array [too_high] = limit
Here's a nice link to enter numup bool indexing.
Source: https://habr.com/ru/post/894988/More articles:Is basic HTTP authentication in CouchDB secure enough for replication in EC2 regions? - securityHow to place a hyperlink "two levels higher in the directory tree"? - htmlzero column space consumption in sqlite db - sqliteBit-Based BinaryWriter in C # - c #Scala: What is the easiest way to get all leaf nodes and their paths in XML? - xmlHow to create / simulate a persistent TCP connection? - c #How to abort a .NET task? - c #powershell: ftp directory listing (script help) - windowsGiven the Ruby metaclass, how do I get the instance to which it is attached? - ruby โโ| fooobar.comAutomatically indent a list of arguments on multiple lines in Visual Studio - code-formattingAll Articles