in python, compare two array in element and change the lower value to zero -


now have 2 arrays

a = [[1.,2.,3.,4.,5.,6.,7.,8.,9.],[9.,8.,7.,6.,5.,4.,3.,2.,1.]]  b = [[2.,2.,1.,2.,0.,2.,1.,2.,0.],[2.,2.,1.,2.,0.,2.,1.,2.,0.]] 

is there fast way achieve following function: compare these 2 arrays element, , change array a[] in following rule:

if a[i,j] > b[i,j]:       a[i,j] = a[i,j]   else     a[i,j] = 0 

i know loop can it, wondering if there other faster way it.

import numpy np = np.array([[1.,2.,3.,4.,5.,6.,7.,8.,9.],[9.,8.,7.,6.,5.,4.,3.,2.,1.]]) b = np.array([[2.,2.,1.,2.,0.,2.,1.,2.,0.],[2.,2.,1.,2.,0.,2.,1.,2.,0.]])  gr = np.greater(a,b) = np.multiply(a,gr) 

the gr , arrays become:

[[false false  true  true  true  true  true  true  true]  [ true  true  true  true  true  true  true false  true]]  array([[ 0.,  0.,  3.,  4.,  5.,  6.,  7.,  8.,  9.],        [ 9.,  8.,  7.,  6.,  5.,  4.,  3.,  0.,  1.]]) 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -