简体   繁体   中英

Subtract 10000 from only large elements in an array

I have an array called "array", which is size (45, 41), which should contain values in the range (-200, 200). However, some of the values have been 'tagged' by the addition of 10,000 to their value. I want to detag those elements by: subtracting 10000 if the element in question is greater than 8000 (if it's greater than 8000 it must be tagged).

So essentially, in pseudocode, I wish to:

for i in 1:45
    for j in 1:41
        if array[i,j] > 8000
            array[i,j] = array[i,j] - 10000

Any help would be greatly appreciated, thanks a lot!

EDIT: Here is my full code:

#Read file
cubes=iris.load(pathfile)
print cubes
wind=cubes[0]
print wind

#Select the month
wind_cut = wind[11, :, :, 0]
array=wind_cut.data

print array.shape

for i in 1:45
    for j in 1:41
        if array[i,j] > 8000
            array[i,j] = array[i,j] - 10000

All I get is an indent error. I'm new to python and this site, sorry if I'm being a noob.

如果您正在使用numpy数组,请尝试以下操作:

array[array>8000] -= 10000

In numpy, a condition like nparray>=N creates an array of true/false we can use as an index.

import numpy
nparray = numpy.array(array) # reads a regular array and makes a numpy array
nparray[nparray>=8000] -=  10000  # subtract 10000 from the elements over 8000 only
# result in nparray

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM