# Vectorize and parallelize wsample function

**URL:** <https://discourse.julialang.org/t/vectorize-and-parallelize-wsample-function/27720>\
**Category:** New to Julia\
**Created:** [August 19, 2019, 4:16pm UTC](https://discourse.julialang.org/t/vectorize-and-parallelize-wsample-function/27720 "2019-08-19T16:16:48Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 19, 2019, 4:53pm UTC](https://discourse.julialang.org/t/vectorize-and-parallelize-wsample-function/27720/2 "2019-08-19T16:53:56Z")

</div>

The first question to ask is: why do you want to “vectorize” the code in this way? Loops in Julia are fast, and if you have a working loop there is no need to change it into a map or broadcast call unless that helps you organize or structure your code more clearly.

Assuming that you do want to use map or broadcast, then you need to tell Julia to treat your `prob` not as a container of individual floats but as a collection of rows. For example, you could broadcast over `eachrow(prob)` rather than `prob`.

However, take a look at the answer by `@stevengj` in [Some doubts about types - #6 by stevengj](https://discourse.julialang.org/t/some-doubts-about-types/27635/6) which also applies in your situation. It is common practice in Matlab or numpy to express a collection of vectors as a matrix, but that’s neither ideal nor necessary in Julia. For example, if you were to transpose your `prob` data and treat it as a collection of `SVectors` from StaticArrays, then you could easily broadcast over it:

```julia
julia> prob = [SVector(.4, .5, .9), SVector(.6, .5, .1)]
2-element Array{SArray{Tuple{3},Float64,1,3},1}:
 [0.4, 0.5, 0.9]
 [0.6, 0.5, 0.1]

julia> sum.(prob)
2-element Array{Float64,1}:
 1.8               
 1.2000000000000002

```

There is no difference in memory layout between a matrix where each column is an element or a vector of SVectors representing those columns, but it is much easier to operate on each element if you actually represent your collection of vectors as a collection of vectors.

---

_[View the full topic](https://discourse.julialang.org/t/vectorize-and-parallelize-wsample-function/27720)._
