# Syntax for constructing a 1x1 matrix

**URL:** https://discourse.julialang.org/t/syntax-for-constructing-a-1x1-matrix/5207
**Category:** General Usage
**Created:** [August 3, 2017, 5:15pm UTC](https://discourse.julialang.org/t/syntax-for-constructing-a-1x1-matrix/5207 "2017-08-03T17:15:25Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)
#### Post date: [August 3, 2017, 5:15pm UTC](https://discourse.julialang.org/t/syntax-for-constructing-a-1x1-matrix/5207/1 "2017-08-03T17:15:25Z")

</div>

Before taking transposes seriously one could conveniently use e.g. `[5]'` to build a 1x1 matrix containing `5`. In v0.6+ this results in a `RowVector` instead. Do we have any compact syntax for building initialized 1x1 matrices now?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [August 3, 2017, 5:19pm UTC](https://discourse.julialang.org/t/syntax-for-constructing-a-1x1-matrix/5207/2 "2017-08-03T17:19:44Z")

</div>

`hcat(1)` ☹

---

<div class="post-metadata">

### Author: ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)
#### Post date: [August 3, 2017, 5:20pm UTC](https://discourse.julialang.org/t/syntax-for-constructing-a-1x1-matrix/5207/3 "2017-08-03T17:20:44Z")

</div>

That’s not half-bad! Thanks!

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [August 3, 2017, 6:09pm UTC](https://discourse.julialang.org/t/syntax-for-constructing-a-1x1-matrix/5207/4 "2017-08-03T18:09:04Z")

</div>

The following also works

```julia
julia> [5][:,:]
1×1 Array{Int64,2}:
 5

```

Still ugly… 😉

---

<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: [August 3, 2017, 6:56pm UTC](https://discourse.julialang.org/t/syntax-for-constructing-a-1x1-matrix/5207/5 "2017-08-03T18:56:04Z")

</div>

But functions can be defined for a reason (and compilers inline for a reason), so:

```julia
julia> mat(sc) = reshape([sc],1,1)
mat (generic function with 1 method)

julia> mat(5)
1×1 Array{Int64,2}:
 5

```

and better yet to consider:

```julia
julia> using StaticArrays

julia> smat(sc) = @SMatrix [sc]
smat (generic function with 1 method)

julia> smat(5)
1×1 StaticArrays.SArray{Tuple{1,1},Int64,2,1}:
 5

```

The little `@code_llvm` I looked at, made these methods preferable and ultimately more readable and correct.

---

<div class="post-metadata">

### Author: ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)
#### Post date: [August 4, 2017, 1:09pm UTC](https://discourse.julialang.org/t/syntax-for-constructing-a-1x1-matrix/5207/6 "2017-08-04T13:09:49Z")

</div>

The `smat` function indeed provides an extremely compact compilation, followed by `hcat`, followed by `reshape` (which is quite suboptimal). Marked this as the best solution, thanks!

EDIT: in cases where a conventional `Matrix` instead of `SMatrix` is preferred, the function

```julia
mat(s) = Matrix(@SMatrix [s])

```

is surprisingly efficient too, better even than `hcat`

```julia
julia> @code_llvm mat(1)

define %jl_value_t addrspace(10)* @julia_mat_62521(i64) #0 !dbg !5 {
top:
  %"#temp#" = alloca %SArray.10, align 8
  %1 = getelementptr inbounds %SArray.10, %SArray.10* %"#temp#", i64 0, i32 0, i64 0
  store i64 %0, i64* %1, align 8
  %2 = addrspacecast %SArray.10* %"#temp#" to %SArray.10 addrspace(11)*
  %3 = call %jl_value_t addrspace(10)* @julia_convert_62498(%jl_value_t addrspace(10)* addrspacecast (%jl_value_t* inttoptr (i64 4464354096 to %jl_value_t*) to %jl_value_t addrspace(10)*), %SArray.10 addrspace(11)* nocapture readonly %2)
  ret %jl_value_t addrspace(10)* %3

```

(I just love StaticArrays.jl !)

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [August 4, 2017, 1:26pm UTC](https://discourse.julialang.org/t/syntax-for-constructing-a-1x1-matrix/5207/7 "2017-08-04T13:26:31Z")

</div>

You do have a function call in there so not sure how you can say whether it is efficient or not.  
Benchmarking them I found that `hcat` and your `mat` had pretty much identical performance .

---

<div class="post-metadata">

### Author: ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)
#### Post date: [August 4, 2017, 1:36pm UTC](https://discourse.julialang.org/t/syntax-for-constructing-a-1x1-matrix/5207/8 "2017-08-04T13:36:02Z")

</div>

Oops, you’re completely right. I’m no good reading llvm 😁. Actually `hcat` is in 25% faster than `mat` in my system! Still, `smat` is way faster than both.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [August 4, 2017, 1:56pm UTC](https://discourse.julialang.org/t/syntax-for-constructing-a-1x1-matrix/5207/9 "2017-08-04T13:56:39Z")

</div>

A side note, it might be a good idea to avoid depending on the extra package `StaticArrays` if not otherwise needed.

---

<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: [August 4, 2017, 2:05pm UTC](https://discourse.julialang.org/t/syntax-for-constructing-a-1x1-matrix/5207/10 "2017-08-04T14:05:12Z")

</div>

> [@Dan](#):
>
> reshape([sc],1,1)

```julia
using BenchmarkTools
julia> @btime reshape([1],1,1)
  63.990 ns (3 allocations: 192 bytes)
1×1 Array{Int64,2}:
 1

julia> @btime fill(1, (1,1))
  29.674 ns (1 allocation: 96 bytes)
1×1 Array{Int64,2}:
 1

julia> @btime hcat(1)
  33.067 ns (1 allocation: 96 bytes)
1×1 Array{Int64,2}:
 1

```

The difference between `hcat` and `fill` is small, but consistent. I also find `fill` more elegant.

---

<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: [August 4, 2017, 2:16pm UTC](https://discourse.julialang.org/t/syntax-for-constructing-a-1x1-matrix/5207/11 "2017-08-04T14:16:55Z")

</div>

`fill` is my favorite too.

With a function:

```julia
mat(s) = begin
       m = Array{typeof(s)}(1,1)
       @inbounds m[1] = s
       return m
end

```

another 10% can be squeezed (on my system, Julia version, and LLVM version):

```julia
julia> @btime mat(5)
  26.736 ns (1 allocation: 96 bytes)
1×1 Array{Int64,2}:
 5

julia> @btime fill(5,(1,1))
  28.640 ns (1 allocation: 96 bytes)
1×1 Array{Int64,2}:
 5

```

And the `@code_llvm` looks nice and short.

---

<div class="post-metadata">

### Author: ![garrison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/garrison/32/209519_2.png) [@garrison](https://discourse.julialang.org/u/garrison)
#### Post date: [August 5, 2017, 5:57am UTC](https://discourse.julialang.org/t/syntax-for-constructing-a-1x1-matrix/5207/13 "2017-08-05T05:57:30Z")

</div>

I typically use `diagm([5])`.

---

<div class="post-metadata">

### Author: ![olivierverdier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olivierverdier/32/50887_2.png) [@olivierverdier](https://discourse.julialang.org/u/olivierverdier)
#### Post date: [June 20, 2023, 12:08pm UTC](https://discourse.julialang.org/t/syntax-for-constructing-a-1x1-matrix/5207/14 "2023-06-20T12:08:07Z")

</div>

This works, for [array literals](https://docs.julialang.org/en/v1/manual/arrays/#man-array-literals):

```julia
[5;;]

```

The number of semicolons indicate the rank of the final tensor, so for instance, `[5;;;]` is a 1x1x1 tensor.
