elementwise-operations

Comparing two NumPy arrays for equality, element-wise

亡梦爱人 提交于 2019-11-26 01:42:46
问题 What is the simplest way to compare two NumPy arrays for equality (where equality is defined as: A = B iff for all indices i: A[i] == B[i] )? Simply using == gives me a boolean array: >>> numpy.array([1,1,1]) == numpy.array([1,1,1]) array([ True, True, True], dtype=bool) Do I have to and the elements of this array to determine if the arrays are equal, or is there a simpler way to compare? 回答1: (A==B).all() test if all values of array (A==B) are True. Note: maybe you also want to test A and B

Comparing two numpy arrays for equality, element-wise

╄→гoц情女王★ 提交于 2019-11-26 00:38:59
What is the simplest way to compare two numpy arrays for equality (where equality is defined as: A = B iff for all indices i: A[i] == B[i] )? Simply using == gives me a boolean array: >>> numpy.array([1,1,1]) == numpy.array([1,1,1]) array([ True, True, True], dtype=bool) Do I have to and the elements of this array to determine if the arrays are equal, or is there a simpler way to compare? (A==B).all() test if all values of array (A==B) are True. Edit (from dbaupp's answer and yoavram's comment) It should be noted that: this solution can have a strange behavior in a particular case: if either A

Element-wise array replication in Matlab

让人想犯罪 __ 提交于 2019-11-26 00:29:13
问题 Let\'s say I have a one-dimensional array: a = [1, 2, 3]; Is there a built-in Matlab function that takes an array and an integer n and replicates each element of the array n times? For example calling replicate(a, 3) should return [1,1,1,2,2,2,3,3,3] . Note that this is not at all the same as repmat . I can certainly implement replicate by doing repmat on each element and concatenating the result, but I am wondering if there is a built in function that is more efficient. 回答1: As of R2015a ,