Questions tagged [elementwise-operations]
204 questions
9
votes
3 answers
How does one perform the exp() operation element-wise in Juila?
I'm new to Julia and this seems like a straight-forward operation but for some reason I am not finding the answer anywhere.
I have been going through some tutorials online and they simply use exp(A) where A is a nxm matrix but this gives me a…

Jeremy
- 123
- 2
- 8
9
votes
3 answers
Elementwise multiplication of arrays in F#
Is there a simple way to multiply the items of an array in F#?
So for example of I want to calculate a population mean from samples I would multiply observed values by frequency and then divide by the sample numbers.
let array_1 =…

Simon Hayward
- 694
- 11
- 26
8
votes
1 answer
Fastest way to take elementwise sum of two Lists
I can do elementwise operation like sum using Zipped function. Let I have two Lists L1 and L2 as shown below
val L1 = List(1,2,3,4)
val L2 = List(5,6,7,8)
I can take element wise sum in following way
(L1,L2).zipped.map(_+_)
and result is
List(6,…

Asif
- 763
- 8
- 18
8
votes
4 answers
Element-wise array replication according to a count
My question is similar to this one, but I would like to replicate each element according to a count specified in a second array of the same size.
An example of this, say I had an array v = [3 1 9 4], I want to use rep = [2 3 1 5] to replicate the…

merv
- 1,449
- 3
- 13
- 25
7
votes
3 answers
Elementwise maximum of sparse Scipy matrix & vector with broadcasting
I need a fast element-wise maximum that compares each row of an n-by-m scipy sparse matrix element-wise to a sparse 1-by-m matrix. This works perfectly in Numpy using np.maximum(mat, vec) via Numpy's broadcasting.
However, Scipy's .maximum() does…

cataclysmic
- 337
- 2
- 12
7
votes
1 answer
Element wise operations array julia
I am a new julia user and I am trying to get a feeling on what is the best pratice to make fast code in julia. I am mainly making element wise operations in arrays/matrices. I tried a few codes to check which one allow me to get the higher…

Dylan
- 107
- 6
6
votes
2 answers
Conditional element replacement using cellfun
vec = randi(10,10,1)
vec(vec < 5) = 0
func = @(x) x(x < 5) = 0 % This isn't valid
How am I supposed to translate the second line of code into a function handle that I can use in conjunction with cellfun?

Andi
- 3,196
- 2
- 24
- 44
6
votes
3 answers
inconsistent results using isreal
Take this simple example:
a = [1 2i];
x = zeros(1,length(a));
for n=1:length(a)
x(n) = isreal(a(n));
end
In an attempt to vectorize the code, I tried:
y = arrayfun(@isreal,a);
But the results are not the same:
x =
1 0
y =
0 …

Dave
- 629
- 3
- 7
- 11
6
votes
1 answer
Element wise comparison between 1D and 2D array
Want to perform an element wise comparison between an 1D and 2D array. Each element of the 1D array need to be compared (e.g. greater) against the corresponding row of 2D and a mask will be created. Here is an example:
A =…

maus
- 75
- 1
- 6
5
votes
1 answer
Using the values of a previous "row" in a pandas series
I have a CSV that looks like this (and when brought into a pandas Dataframe with
read_csv(), it looks the same).
I want to update the values in column ad_requests according to the following logic:
For a given row, if ad_requests has a value, leave…

Pyderman
- 14,809
- 13
- 61
- 106
5
votes
1 answer
Parallelization of elementwise matrix multiplication
I'm currently optimizing parts of my code and therefore perform some benchmarking.
I have NxN-matrices A and T and want to multiply them elementwise and save the result in A again, i.e. A = A*T. As this code is not parallelizable I expanded the…

Florian
- 61
- 4
4
votes
1 answer
How to multiply two arrays at specific indexes?
Toy example
I have two arrays, which have different shape, for example:
import numpy as np
matrix = np.arange(5*6*7*8).reshape(5, 6, 7, 8)
vector = np.arange(1, 20, 2)
What I want to do is to multiply each element of the matrix by one of the…

Görg
- 141
- 4
4
votes
1 answer
Python - vector to function is slower than calling for loop on each element. Numpy slower than native Python
I am working on code to perform a transient simulation calling a function for each second of the day and eventually whole year. I thought I found an opportunity to speed up the code by passing a vector of inputs instead of calling a for loop,…

buddy193
- 41
- 1
4
votes
1 answer
Dictionary element-wise operations in Julia
I would like to broadcast an operation to all values of a dictionary. For an array, I know I can broadcast an element-wise operation using:
julia> b1 = [1, 2, 3]
julia> b1./2
3-element Array{Float64,1}:
0.5
1.0
1.5
What is an efficient way of…

Bremsstrahlung
- 686
- 1
- 9
- 23
4
votes
1 answer
elementwise binding in R
I want a function f such that
(outer(X, Y, f))[i, j] is a side-by-side concatenation of the i-th element of X and the j-th element of Y, something like c(X[i], Y[j]), or having a similar structure.
Furthermore, I want this result to be such that the…

kjo
- 33,683
- 52
- 148
- 265