Questions tagged [elementwise-operations]

204 questions
435
votes
8 answers

Comparing two NumPy arrays for equality, element-wise

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, …
clstaudt
  • 21,436
  • 45
  • 156
  • 239
338
votes
17 answers

Element-wise addition of 2 lists?

I have now: list1 = [1, 2, 3] list2 = [4, 5, 6] I wish to have: [1, 2, 3] + + + [4, 5, 6] || || || [5, 7, 9] Simply an element-wise addition of two lists. I can surely iterate the two lists, but I don't want do that. What is the most Pythonic…
Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174
189
votes
15 answers

How to perform element-wise multiplication of two lists?

I want to perform an element wise multiplication, to multiply two lists together by value in Python, like we can do it in Matlab. This is how I would do it in Matlab. a = [1,2,3,4] b = [2,3,4,5] a .* b = [2, 6, 12, 20] A list comprehension would…
xxjjnn
  • 14,591
  • 19
  • 61
  • 94
173
votes
4 answers

How to get element-wise matrix multiplication (Hadamard product) in numpy?

I have two matrices a = np.matrix([[1,2], [3,4]]) b = np.matrix([[5,6], [7,8]]) and I want to get the element-wise product, [[1*5,2*6], [3*7,4*8]], equaling [[5,12], [21,32]] I have tried print(np.dot(a,b)) and print(a*b) but both give the…
Malintha
  • 4,512
  • 9
  • 48
  • 82
70
votes
6 answers

How to multiply all integers inside list

Hello so I want to multiply the integers inside a list. For example; l = [1, 2, 3] l = [1*2, 2*2, 3*2] output: l = [2, 4, 6] So I was searching online and most of the answers were regarding multiply all the integers with each other such…
Andre
  • 1,601
  • 6
  • 19
  • 19
45
votes
7 answers

Element-wise string concatenation in numpy

Is this a bug? import numpy as np a1=np.array(['a','b']) a2=np.array(['E','F']) In [20]: add(a1,a2) Out[20]: NotImplemented I am trying to do element-wise string concatenation. I thought Add() was the way to do it in numpy but obviously it is not…
Dave31415
  • 2,846
  • 4
  • 26
  • 34
40
votes
4 answers

Why is zipped faster than zip in Scala?

I have written some Scala code to perform an element-wise operation on a collection. Here I defined two methods that perform the same task. One method uses zip and the other uses zipped. def ES (arr :Array[Double], arr1 :Array[Double])…
28
votes
5 answers

Are there builtin functions for elementwise boolean operators over boolean lists?

For example, if you have n lists of bools of the same length, then elementwise boolean AND should return another list of that length that has True in those positions where all the input lists have True, and False everywhere else. It's pretty easy to…
bshanks
  • 1,238
  • 2
  • 12
  • 24
20
votes
3 answers

Array elementwise operations

I have two input arrays x and y of the same shape. I need to run each of their elements with matching indices through a function, then store the result at those indices in a third array z. What is the most pythonic way to accomplish this? Right…
user1764386
  • 5,311
  • 9
  • 29
  • 42
19
votes
6 answers

elementwise combination of two lists in R

Say I have two lists: list.a <- as.list(c("a", "b", "c")) list.b <- as.list(c("d", "e", "f")) I would like to combine these lists recursively, such that the result would be a list of combined elements as a vector like the following: [[1]] [1] a…
sinclairjesse
  • 1,585
  • 4
  • 17
  • 29
18
votes
7 answers

Element-wise array replication in Matlab

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…
Dima
  • 38,860
  • 14
  • 75
  • 115
14
votes
1 answer

Differences between Numpy divide and Python divide?

What are the similarities and differences between numpy.divide and the Python slash / operator? As far as I can tell they behave the same, both implementing an element-wise division. The Numpy documentation mentions: numpy.divide(x1, x2) ...…
pooya13
  • 2,060
  • 2
  • 23
  • 29
14
votes
2 answers

numpy elementwise outer product

I want to do the element-wise outer product of two 2d arrays in numpy. A.shape = (100, 3) # A numpy ndarray B.shape = (100, 5) # A numpy ndarray C = element_wise_outer_product(A, B) # A function that does the trick C.shape = (100, 3, 5) # This…
14
votes
9 answers

How can I sum arrays element-wise in Perl?

I have two arrays: @arr1 = ( 1, 0, 0, 0, 1 ); @arr2 = ( 1, 1, 0, 1, 1 ); I want to sum items of both arrays to get new one like ( 2, 1, 0, 1, 2 ); Can I do it without looping through arrays?
Dmytro Leonenko
  • 1,443
  • 5
  • 19
  • 30
13
votes
3 answers

Pandas element-wise min max against a series along one axis

I have a Dataframe: df = A B C D DATA_DATE 20170103 5.0 3.0 NaN NaN 20170104 NaN NaN NaN 1.0 20170105 1.0 NaN 2.0 3.0 And I have a series s = DATA_DATE 20170103 4.0 20170104 0.0 20170105 2.2 I'd like…
Zhang18
  • 4,800
  • 10
  • 50
  • 67
1
2 3
13 14