# Converting between Matrices, Tuples, and Iterators

**URL:** <https://discourse.julialang.org/t/converting-between-matrices-tuples-and-iterators/62596>\
**Category:** New to Julia\
**Created:** [June 8, 2021, 10:33pm UTC](https://discourse.julialang.org/t/converting-between-matrices-tuples-and-iterators/62596 "2021-06-08T22:33:49Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![GpuCoder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gpucoder/32/24511_2.png) [@GpuCoder](https://discourse.julialang.org/u/GpuCoder)\
**Post date:** [June 8, 2021, 10:33pm UTC](https://discourse.julialang.org/t/converting-between-matrices-tuples-and-iterators/62596/1 "2021-06-08T22:33:49Z")

</div>

So there is something unstated about the way matrices, tuples, and iterators (i.e., Base.Generator) work that is causing me problems. I can make the hard things work (except for a few installation issues), but I have trouble almost every day with the equivalent of `for` loops or vectorization.

Today’s example is that I’m trying to build a matrix. The details of the problem are complicated, but the complicated stuff works fine. The simplification of the stuff that doesn’t work is:

```
samps = [3, 2, 5, 8, 3]
5-element Vector{Int64}:
 3
 2
 5
 8
 3

temp = [map(x -> x^k, samps) for k = 1:3]
3-element Vector{Vector{Int64}}:
 [3, 2, 5, 8, 3]
 [9, 4, 25, 64, 9]
 [27, 8, 125, 512, 27]

```

Great, but I wanted a matrix. Should be a trite change, but I’ve tried about a dozen different things and have yet to figure out something that works. Two examples of things I tried:

```
convert(Matrix{Int64}, temp)
MethodError: no method matching Matrix{Int64}(::Vector{Vector{Int64}})

```

and

```
temp1 = vcat(map(x -> x^k, samps) for k = 1:3)
1-element Vector{Base.Generator{UnitRange{Int64}, var"#5#7"}}:
 Base.Generator{UnitRange{Int64}, var"#5#7"}(var"#5#7"(), 1:3)

collect(temp1)
1-element Vector{Base.Generator{UnitRange{Int64}, var"#5#7"}}:
 Base.Generator{UnitRange{Int64}, var"#5#7"}(var"#5#7"(), 1:3)

```

I also looked for a fill like solution, but didn’t find it.

The best solution would be to create the matrix directly with a one liner, without copy, and without conversion. I don’t need the fastest possible solution (this is not the Focal Point of Amdahl’s Law for the larger application), within a factor of 2 or 3 of C would be great.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [June 8, 2021, 10:40pm UTC](https://discourse.julialang.org/t/converting-between-matrices-tuples-and-iterators/62596/2 "2021-06-08T22:40:35Z")

</div>

First, here is something that does what you want (I believe)

```julia
julia> samps'.^(1:3)
3×5 Matrix{Int64}:
  3 2 5 8 3
  9 4 25 64 9
 27 8 125 512 27

```

Now you can decide if that needs to be sped up or not.

---

<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:** [June 8, 2021, 11:00pm UTC](https://discourse.julialang.org/t/converting-between-matrices-tuples-and-iterators/62596/3 "2021-06-08T23:00:59Z")

</div>

An array comprehension seems appropriate

```julia
[x^k for x in samp, k in 1:3]

```

Warning: this was written on my phone and is untested, but it’s close.

Your `temp` variable must necessarily be a vector of vectors, by construction, but you can use `hcat(temp...)` or `reduce(hcat, temp)`.

---

<div class="post-metadata">

**Author:** ![GpuCoder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gpucoder/32/24511_2.png) [@GpuCoder](https://discourse.julialang.org/u/GpuCoder)\
**Post date:** [June 9, 2021, 12:50am UTC](https://discourse.julialang.org/t/converting-between-matrices-tuples-and-iterators/62596/4 "2021-06-09T00:50:21Z")

</div>

> [@tbeason](#):
>
> First, here is something that does what you want (I believe)
> 
> ```julia
> julia> samps'.^(1:3)
> 3×5 Matrix{Int64}:
> 3 2 5 8 3
> 9 4 25 64 9
> 27 8 125 512 27
> 
> ```

Oh no, I’ve oversimplified . I meant for this to be a stand-in for a set of nearly arbitrary functions, powers of integers are too simple.

---

<div class="post-metadata">

**Author:** ![GpuCoder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gpucoder/32/24511_2.png) [@GpuCoder](https://discourse.julialang.org/u/GpuCoder)\
**Post date:** [June 9, 2021, 1:25am UTC](https://discourse.julialang.org/t/converting-between-matrices-tuples-and-iterators/62596/5 "2021-06-09T01:25:24Z")

</div>

> [@DNF](#):
>
> `[x^k for x in samp, k in 1:3]`

That works. It is what I needed.

But before posting I had tried:

```
[x^k for x in samp, for k in 1:3]
syntax: unexpected "]"

```

What is the deeper understanding of the syntax (or semantics) that will generalize this example?

---

<div class="post-metadata">

**Author:** ![GpuCoder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gpucoder/32/24511_2.png) [@GpuCoder](https://discourse.julialang.org/u/GpuCoder)\
**Post date:** [June 9, 2021, 7:50pm UTC](https://discourse.julialang.org/t/converting-between-matrices-tuples-and-iterators/62596/6 "2021-06-09T19:50:20Z")

</div>

> [@DNF](#):
>
> An array comprehension seems appropriate
> 
> ```julia
> [x^k for x in samp, k in 1:3]
> 
> ```

Not using `map` in some contexts seems like an important pattern. I see no reason to avoid something like:

```
[funcTab[k](x) for x in samp, k in 1:3 ]

```

Using `map` has become a habit (☹ I wonder when that happened to me). I’ve been using a CAS (Computer Algebra System) where you do something like

```
[UnArray(map(x -> x^k, samp)) for k in 0:2]

```

where `UnArray` converts an array to a sequence that is repackaged into the new array. This may be inherently inefficient, because it forces copying (or for some other reason) … but if I naturally end up with a row array from the inner loop is there something like this that I can (or should) use?

---

<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:** [June 9, 2021, 9:04pm UTC](https://discourse.julialang.org/t/converting-between-matrices-tuples-and-iterators/62596/7 "2021-06-09T21:04:46Z")

</div>

> [@GpuCoder](#):
>
> UnArray

You can ‘unarray’ with the splat operator, `...`. I dont’t see how that would help here though. I doesn’t work in this context, but even if it did, what would be the interpretation of something like

```julia
[1, 2, 3 for i in 0:2]

```

?  
I don’t know what that should do.

---

<div class="post-metadata">

**Author:** ![GpuCoder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gpucoder/32/24511_2.png) [@GpuCoder](https://discourse.julialang.org/u/GpuCoder)\
**Post date:** [June 10, 2021, 12:43am UTC](https://discourse.julialang.org/t/converting-between-matrices-tuples-and-iterators/62596/8 "2021-06-10T00:43:58Z")

</div>

> [@DNF](#):
>
> ```julia
> [1, 2, 3 for i in 0:2]
> 
> ```
> 
> ?  
> I don’t know what that should do.

Apparently it doesn’t mean anything. But how do I make it mean what I want?

Before you ask what I want, let me try to restate … If for some reason that’s a bit flaky (perhaps involving other people’s code) I produce a Vector of Vectors, but I needed the corresponding matrix, what is the best way to convert?

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [June 10, 2021, 2:45am UTC](https://discourse.julialang.org/t/converting-between-matrices-tuples-and-iterators/62596/9 "2021-06-10T02:45:07Z")

</div>

If `X` is your vector of vectors, `reduce(hcat,X)` will return the transpose of what you desired in the first post.

```julia
julia> reduce(hcat,X)
5×3 Matrix{Int64}:
 3 9 27
 2 4 8
 5 25 125
 8 64 512
 3 9 27

julia> reduce(hcat,X)'
3×5 adjoint(::Matrix{Int64}) with eltype Int64:
  3 2 5 8 3
  9 4 25 64 9
 27 8 125 512 27

```

You can also just easily write your own function.

```julia
function tomatrix(X::Vector{Vector{T}}) where {T}
    N = length(X)
    K = length(first(X))
    Z = Matrix{T}(undef,K,N)
    @inbounds for (i,v) in pairs(X)
        for (j,y) in pairs(v)
            Z[j,i] = y
        end
    end
    return Z
end

```

---

<div class="post-metadata">

**Author:** ![GpuCoder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gpucoder/32/24511_2.png) [@GpuCoder](https://discourse.julialang.org/u/GpuCoder)\
**Post date:** [June 10, 2021, 1:41pm UTC](https://discourse.julialang.org/t/converting-between-matrices-tuples-and-iterators/62596/10 "2021-06-10T13:41:47Z")

</div>

Thanks. The first thing I tried was something close to this, but it must have been slightly wrong in a way that I didn’t see at the time.

Will solder on. My intuition will catch up with the rest of me, eventually.

😪

---

<div class="post-metadata">

**Author:** ![GpuCoder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gpucoder/32/24511_2.png) [@GpuCoder](https://discourse.julialang.org/u/GpuCoder)\
**Post date:** [June 10, 2021, 2:07pm UTC](https://discourse.julialang.org/t/converting-between-matrices-tuples-and-iterators/62596/11 "2021-06-10T14:07:15Z")

</div>

> [@GpuCoder](#):
>
> The first thing I tried was something close to this, but it must have been slightly wrong in a way that I didn’t see at the time.

As I look back over my notes, I notice that I’m often wrong in my choice between `hcat` and `vcat` (though there are other issues as well). I think this arises because a Vector of Vectors displays the inner vectors as rows (the convection that is common for graphics) but they are actually columns (the convention that is common for “Linear Algebra”).

Not sure how often this confusion arises for those new to Julia; not sure what in the manual would have helped.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 10, 2021, 3:41pm UTC](https://discourse.julialang.org/t/converting-between-matrices-tuples-and-iterators/62596/12 "2021-06-10T15:41:41Z")

</div>

I really am not sure if this is better but what about:

```julia
julia> reshape([y for k in 0:2 for y in map(x -> x^k, samp)], 3, length(samp))
3×5 Array{Int64,2}:
 1 1 2 3 25
 1 1 5 9 64
 1 3 8 4 9

```

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [June 10, 2021, 5:32pm UTC](https://discourse.julialang.org/t/converting-between-matrices-tuples-and-iterators/62596/13 "2021-06-10T17:32:13Z")

</div>

> [@GpuCoder](#):
>
> I think this arises because a Vector of Vectors displays the inner vectors as rows

Yeah, I can see how that could cause some confusion. I guess the thing to keep in mind is that `[1, 2, 3]` always represents a regular old vector (i.e., a tensor of order/rank 1). Julian vectors are neither row vectors nor column vectors. This could be confusing if you’re coming from Matlab where `[1, 2, 3] == [1 2 3]`. In other words, in Matlab `[1, 2, 3]` is a row vector (a matrix of size `(1, 3)`).

---

<div class="post-metadata">

**Author:** ![GpuCoder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gpucoder/32/24511_2.png) [@GpuCoder](https://discourse.julialang.org/u/GpuCoder)\
**Post date:** [June 22, 2021, 7:23pm UTC](https://discourse.julialang.org/t/converting-between-matrices-tuples-and-iterators/62596/14 "2021-06-22T19:23:10Z")

</div>

I don’t think of myself as “coming from Matlab”, but I’ve done quite a bit of Matlab, and was thinking about vectors as “row-matrices” or “column-matrices”, and was further imagining that the way they were displayed indicated something about orientation … strange how background can affect the little things more than the big things

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [June 22, 2021, 9:41pm UTC](https://discourse.julialang.org/t/converting-between-matrices-tuples-and-iterators/62596/15 "2021-06-22T21:41:31Z")

</div>

> [@GpuCoder](#):
>
> Oh no, I’ve oversimplified . I meant for this to be a stand-in for a set of nearly arbitrary functions, powers of integers are too simple.

For an arbitrary function `f(x, k)`, you can do `f.(samp, (1:3)')`. For example,

```julia
samp = [3, 2, 5, 8, 3]
f(x, k) = x^k
f.(samp, (1:3)')

```

The result is

```julia
5×3 Matrix{Int64}:
 3 9 27
 2 4 8
 5 25 125
 8 64 512
 3 9 27

```

The combination of the broadcast dot syntax and the adjoint `y'` of a vector `y` is very useful for constructing matrices.
