Wednesday, 15 April 2015

numpy - python equivalent of MATLAB statement A(B==1)= C -



numpy - python equivalent of MATLAB statement A(B==1)= C -

i have 3 numpy arrays follows:

a = [1, 2, 3, 4, 5] b = [0, 1, 0, 0, 1] c = [30, 40]

i replace elements of equivalent in b equal 1. above illustration this:

a = [1, 30, 3, 4, 40]

in matlab, can this:

a(b==1) = c'

do know equivalent code in python (preferably works when , b multidimensional too)? in advance.

the syntax pretty similar:

>>> import numpy np >>> = np.array([1, 2, 3, 4, 5]) >>> b = np.array([0, 1, 0, 0, 1]) >>> c = np.array([30, 40]) >>> a[b==1] = c >>> array([ 1, 30, 3, 4, 40])

python numpy indexing

No comments:

Post a Comment