# Point wise operations on array of vectors

**URL:** <https://discourse.julialang.org/t/point-wise-operations-on-array-of-vectors/39611>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 17, 2020, 2:10am UTC](https://discourse.julialang.org/t/point-wise-operations-on-array-of-vectors/39611 "2020-05-17T02:10:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bionicinnovations1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bionicinnovations1/32/22874_2.png) [@bionicinnovations1](https://discourse.julialang.org/u/bionicinnovations1)\
**Post date:** [May 17, 2020, 2:10am UTC](https://discourse.julialang.org/t/point-wise-operations-on-array-of-vectors/39611/1 "2020-05-17T02:10:59Z")

</div>

I have an array of vectors (of different lengths) and I want to perform a point wise operation on all values:

a= [[1 2 3 4], [1 2 3 4 5], [1 2 3]]

b = a .-1

This give me an error.

MethodError: no method matching -(::Array{Int64,2}, ::Int64)  
Closest candidates are:  
-(!Matched::Complex{Bool}, ::Real) at complex.jl:307  
-(!Matched::Missing, ::Number) at missing.jl:115  
-(!Matched::Base.CoreLogging.LogLevel, ::Integer) at logging.jl:108  
…

Stacktrace:  
[1] \_broadcast\_getindex\_evalf at .\broadcast.jl:631 [inlined]  
[2] \_broadcast\_getindex at .\broadcast.jl:604 [inlined]  
[3] getindex at .\broadcast.jl:564 [inlined]  
[4] copy at .\broadcast.jl:854 [inlined]  
[5] materialize(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(-),Tuple{Array{Array{Int64,2},1},Int64}}) at .\broadcast.jl:820  
[6] top-level scope at In[95]:8

How do I perform this operation simply without a loop?

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [May 17, 2020, 2:49am UTC](https://discourse.julialang.org/t/point-wise-operations-on-array-of-vectors/39611/2 "2020-05-17T02:49:18Z")

</div>

```julia
julia> a= [[1 2 3 4], [1 2 3 4 5], [1 2 3]]
3-element Array{Array{Int64,2},1}:
 [1 2 3 4]
 [1 2 … 4 5]
 [1 2 3]

julia> broadcast(x -> x .+= 1, a)
3-element Array{Array{Int64,2},1}:
 [2 3 4 5]
 [2 3 … 5 6]
 [2 3 4]

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 17, 2020, 5:49am UTC](https://discourse.julialang.org/t/point-wise-operations-on-array-of-vectors/39611/3 "2020-05-17T05:49:10Z")

</div>

Also

```julia
julia> (x -> x .- 1).(a)
3-element Array{Array{Int64,2},1}:
 [0 1 2 3]
 [0 1 … 3 4]
 [0 1 2]

```

Incidentally, please quote your code.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 17, 2020, 5:50am UTC](https://discourse.julialang.org/t/point-wise-operations-on-array-of-vectors/39611/4 "2020-05-17T05:50:31Z")

</div>

> [@jishnub](#):
>
> `broadcast(x -> x .+= 1, a)`

Note that this will modify the original elements of `a`, which may not have been intended.

---

<div class="post-metadata">

**Author:** ![bionicinnovations1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bionicinnovations1/32/22874_2.png) [@bionicinnovations1](https://discourse.julialang.org/u/bionicinnovations1)\
**Post date:** [May 18, 2020, 1:38am UTC](https://discourse.julialang.org/t/point-wise-operations-on-array-of-vectors/39611/5 "2020-05-18T01:38:27Z")

</div>

thanks, greatly appreciated!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 18, 2020, 4:21am UTC](https://discourse.julialang.org/t/point-wise-operations-on-array-of-vectors/39611/6 "2020-05-18T04:21:03Z")

</div>

You can also use a comprehension:

```julia
b = [x .- 1 for x in a]

```

> [@bionicinnovations1](#):
>
> How do I perform this operation simply without a loop?

If you are thinking about performance, there is no reason to avoid loops, they are generally the fastest way of doing things.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [May 18, 2020, 7:05am UTC](https://discourse.julialang.org/t/point-wise-operations-on-array-of-vectors/39611/7 "2020-05-18T07:05:43Z")

</div>

> [@DNF](#):
>
> If you are thinking about performance, there is no reason to avoid loops, they are generally the fastest way of doing things.

While this is likely true in this particular case, loops can often be pretty far from the fastest way. Think `dot` from BLAS vs for-loop implementation - even for vectors a proper `dot` is several times faster.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 18, 2020, 7:25am UTC](https://discourse.julialang.org/t/point-wise-operations-on-array-of-vectors/39611/8 "2020-05-18T07:25:04Z")

</div>

This doesn’t really have anything to do with loops, as the BLAS functions are also implemented with loops. They are just using various extra clever techniques to make sure the loops simd-vectorize and are cache optimal, multithreaded, etc. etc.

For example:

```julia
function mydot(x, y)
    length(x) == length(y) || error("Incompatible lengths")
    out = zero(eltype(x))
    @simd for i in eachindex(x, y)
        @inbounds out += x[i] * y[i]
    end
    return out
end

julia> using LinearAlgebra, BenchmarkTools

julia> @btime dot(x, y) setup=(x=rand(10_000); y=rand(10_000))
  2.098 μs (0 allocations: 0 bytes)

julia> @btime mydot(x, y) setup=(x=rand(10_000); y=rand(10_000))
  2.090 μs (0 allocations: 0 bytes)

```

For longer vectors, multithreading becomes important, and I should add that to my loop to keep up with `LinearAlgebra.dot`, but it’s all loops.

My point being: Sometimes someone has worked hard on optimally implementing a really fast algorithm, and of course then you should call their function. But there is no reason to use broadcasting or `map` or comprehensions, for the sake of performance _only_. Rolling your own version with a straight loop has every chance of matching or outperforming these other solutions.
