[SciPy-User] [scipy-user] How to apply a condition on some specific values of an array
Mon Feb 6 04:27:20 CST 2012
Great, it seems really easy!
Is it possible to append the values to new_arr because I have to do it
with many "arr" so new_arr will be erase each time if I do:
new_arr[arr < 500] *= 100
new_arr[arr2 < 500] *= 100
new_arr[arr3 < 500] *= 100
I've tried np.append but it doesn't work...
Nevertheless thanks again!
2012/2/6 Scott Sinclair <firstname.lastname@example.org>:
> On 6 February 2012 11:41, Fabien Lafont <email@example.com> wrote:
>> And is it possible to apply a specific operation with a condition
>> (if). I have to apply different operations on the same array depending
>> on the value of the array element.
>> For example: I have an array like that:
>> [100 250 501 700] and I want to multiply by 100 the value if this
>> value is smalest thant 500 and multiply by 1000 if the value is
> Here's one way that should be easy to follow. You'll have to make a
> copy of your array (as shown here), or generate two index arrays
> before modifying your original array.
> In : arr = np.array([100, 250, 501, 700])
> In : # make a copy to avoid aliasing
> ...: new_arr = np.array(arr)
> In : new_arr[arr < 500] *= 100
> In : new_arr[arr > 500] *= 1000
> In : arr
> Out: array([100, 250, 501, 700])
> In : new_arr
> Out: array([ 10000, 25000, 501000, 700000])
> SciPy-User mailing list
More information about the SciPy-User