# Non-catting version of matrix construction

**URL:** <https://discourse.julialang.org/t/non-catting-version-of-matrix-construction/4205>\
**Category:** General Usage\
**Created:** [June 11, 2017, 7:03am UTC](https://discourse.julialang.org/t/non-catting-version-of-matrix-construction/4205 "2017-06-11T07:03:57Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [June 11, 2017, 7:03am UTC](https://discourse.julialang.org/t/non-catting-version-of-matrix-construction/4205/1 "2017-06-11T07:03:57Z")

</div>

I want to do the following but obtaining a `Matrix{Matrix{Float64}}` not a `Matrix{Float64}`:

```julia
julia> A = rand(2,2);

julia> [A A; A A]
4×4 Array{Float64,2}:
 0.636575 0.458969 0.636575 0.458969 
 0.236709 0.0897579 0.236709 0.0897579
 0.636575 0.458969 0.636575 0.458969 
 0.236709 0.0897579 0.236709 0.0897579

```

The only way to do this in one line that came to mind is the following, which is less than ideal:

```julia
julia> reshape([A,A,A,A],2,2)
2×2 Array{Array{Float64,2},2}:
 [0.636575 0.458969; 0.236709 0.0897579] … [0.636575 0.458969; 0.236709 0.0897579]
 [0.636575 0.458969; 0.236709 0.0897579] [0.636575 0.458969; 0.236709 0.0897579]

```

Any easier way?

---

<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:** [June 11, 2017, 7:30am UTC](https://discourse.julialang.org/t/non-catting-version-of-matrix-construction/4205/2 "2017-06-11T07:30:00Z")

</div>

What about

```julia
fill(rand(2,2), 2, 2)

```

? Though it is unclear whether having identical matrices is part of the spec or just the example.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [June 11, 2017, 7:32am UTC](https://discourse.julialang.org/t/non-catting-version-of-matrix-construction/4205/3 "2017-06-11T07:32:12Z")

</div>

No I don’t actually want identical matrices ( and also not just 2 x 2)

My actual problem is a type that overrides `hvcat` a la `SparseArray`, but I still need to work with matrices of the type.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 11, 2017, 9:26am UTC](https://discourse.julialang.org/t/non-catting-version-of-matrix-construction/4205/4 "2017-06-11T09:26:38Z")

</div>

We don’t currently have a syntax to efficiently do it. One syntax to do it is `[[A] [A]; [A] [A]]`. In principle, this can be optimized in the parser to be efficient too.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [June 11, 2017, 9:32am UTC](https://discourse.julialang.org/t/non-catting-version-of-matrix-construction/4205/5 "2017-06-11T09:32:16Z")

</div>

It could possibly be argued that a `typed_hvcat`

```julia
Matrix{Float64}[A A; A A]

```

should do that but at the moment it doesn’t and I’m not sure whether there are any fundamental problems with the idea.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 11, 2017, 12:34pm UTC](https://discourse.julialang.org/t/non-catting-version-of-matrix-construction/4205/6 "2017-06-11T12:34:41Z")

</div>

No it shouldn’t since it’s still a concatenation.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [June 11, 2017, 2:32pm UTC](https://discourse.julialang.org/t/non-catting-version-of-matrix-construction/4205/7 "2017-06-11T14:32:30Z")

</div>

Still not pretty, but:

```julia
A = rand(2,2)
map(i->A,zeros(2,2))

```

works. And with little effort some other matrices can be constructed, for example:

```julia
B = rand(2,2)
map(i->(A,B)[i],[1 2 ; 2 1])

```

alternates between two sub-matrices.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [June 11, 2017, 7:10pm UTC](https://discourse.julialang.org/t/non-catting-version-of-matrix-construction/4205/8 "2017-06-11T19:10:01Z")

</div>

It sounds like the answer to my question is “no”. I’m going to try to put together a macro

```julia
@nocat [A A ; A A]

```

That should do what I need

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [June 13, 2017, 3:01am UTC](https://discourse.julialang.org/t/non-catting-version-of-matrix-construction/4205/9 "2017-06-13T03:01:01Z")

</div>

This seems to work:

```julia
vnocat(A...) = [A...]
hnocat(A...) = reshape(vnocat(A...),(1,length(A)))
hvnocat(dims,A...) = reshape(vnocat(A...),dims)

macro nocat(x)
    ex = expand(x)
    if ex.args[1] == :vcat
        ex.args[1] = :vnocat
    elseif ex.args[1] == :hcat
        ex.args[1] = :hnocat
    else
        @assert ex.args[1] == :hvcat
        ex.args[1] = :hvnocat
    end
    ex
end

A =rand(2,2)
@assert @nocat([A A; A A]) == reshape([A,A,A,A],(2,2))

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 13, 2017, 3:10am UTC](https://discourse.julialang.org/t/non-catting-version-of-matrix-construction/4205/10 "2017-06-13T03:10:59Z")

</div>

> [@dlfivefifty](#):
>
> ex = expand(x)

This is almost always wrong for a general purpose/expression-like macro. It’ll fail when the expression becomes more complicated (e.g. if you have `?:`). Note that if you don’t care about the performance that much, you should just use `[[A] [A]; [A] [A]]` syntax. It has a high probability of becoming the syntax for this unless someone propose a different one.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [June 13, 2017, 3:26am UTC](https://discourse.julialang.org/t/non-catting-version-of-matrix-construction/4205/11 "2017-06-13T03:26:56Z")

</div>

Ah OK, thanks for the tip. Putting aside whether the macro is a good idea (it turns out I actually just need `hvnocat` function, not the macro), to implement this macro would you suggest working on the original expression? that is, converting `Expr(:vcat,Expr(:row,A,A),Expr(:row,A,A))` to an `hvnocat` call directly?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 13, 2017, 11:37am UTC](https://discourse.julialang.org/t/non-catting-version-of-matrix-construction/4205/12 "2017-06-13T11:37:49Z")

</div>

> [@dlfivefifty](#):
>
> would you suggest working on the original expression?

Yes, that would be the right way to implement such a macro.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [June 13, 2017, 5:47pm UTC](https://discourse.julialang.org/t/non-catting-version-of-matrix-construction/4205/13 "2017-06-13T17:47:58Z")

</div>

> [@yuyichao](#):
>
> It has a high probability of becoming the syntax for this unless someone propose a different one

I can give a “bad” different syntax in the hopes someone proposes a good one:

```julia
[A A,
 A A]

```

that is, mimic `vcat` where ; indicates concatination while , indicates nocat.

I don’t like this proposal because `[A A]` indicates `hcat`, so I’d say the natural interpretation of the proposal is to only concatenate in the horizontal direction. So what’s missing is an alternative to a space to indicate `hnocat`.
