# Effective Solution for transformation Matrix to Vector {Block}

**URL:** <https://discourse.julialang.org/t/effective-solution-for-transformation-matrix-to-vector-block/6037>\
**Category:** General Usage\
**Tags:** question, performance, benchmark\
**Created:** [September 22, 2017, 1:02pm UTC](https://discourse.julialang.org/t/effective-solution-for-transformation-matrix-to-vector-block/6037 "2017-09-22T13:02:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)\
**Post date:** [September 22, 2017, 1:02pm UTC](https://discourse.julialang.org/t/effective-solution-for-transformation-matrix-to-vector-block/6037/1 "2017-09-22T13:02:02Z")

</div>

Hello, I have an exciting problem. Consider type `Block` and `rawData` Matrix.

```julia
struct Block
  name::String
  data::Vector{Float64}
end

```

```julia
headers = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
rawData = rand(8700, 10)
dataRanges = Vector{UnitRange}([1:400, 500:8000])

```

Now we want to transform Matrix into `Vector{Block}` with using data ranges. My naive solution is:

```julia
function fNaive(rawData::Matrix{Float64}, dataRanges::Vector{UnitRange})
  db = map(1:cols(rawData)) do i
    map(r -> Block(headers[i], view(rawData, r, i)), dataRanges)
  end
  
  vcat(db...)
end

```

How to get the most effective solution for `Vector{Block}`, any solution?  
Thanks

 ![blocks](https://global.discourse-cdn.com/julialang/original/3X/d/f/df5a37ed5c62586efdc0ee499e5ee59a8a54974c.png)  
For better imagination, one block is a one-color of the Matrix.

---

<div class="post-metadata">

**Author:** ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)\
**Post date:** [September 25, 2017, 6:52am UTC](https://discourse.julialang.org/t/effective-solution-for-transformation-matrix-to-vector-block/6037/2 "2017-09-25T06:52:10Z")

</div>

I get the impression that this is a very effective solution. I’ve replaced the nested function `map` with comprehension, which gives a little better results.

---

<div class="post-metadata">

**Author:** ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)\
**Post date:** [October 18, 2017, 5:05am UTC](https://discourse.julialang.org/t/effective-solution-for-transformation-matrix-to-vector-block/6037/3 "2017-10-18T05:05:27Z")

</div>

Ou, I forgot to add `cols` function, but that could be clearly.

> cols(X::Array) = size(X, 2)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 18, 2017, 1:42pm UTC](https://discourse.julialang.org/t/effective-solution-for-transformation-matrix-to-vector-block/6037/4 "2017-10-18T13:42:50Z")

</div>

> [@Martin\_Florek](#):
>
> I get the impression that this is a very effective solution.

Depends on what you mean by “effective”. It is probably not the most efficient solution because of the extra arrays and copies via `vcat`. I would just tend to do something like:

```julia
v = Block[]
for i in 1:size(rawData,2), r in dataRanges
    push!(v, Block(headers[i], view(rawData, r, i)))
end
return v

```

which is both shorter and more efficient.

(Coming from other languages, people are often afraid to write loops. Sometimes explicit loops are the clearest code, and they are often the most efficient too!)
