# Creating Single-Entry Matrix?

**URL:** <https://discourse.julialang.org/t/creating-single-entry-matrix/113932>\
**Category:** General Usage\
**Tags:** question, matrices\
**Created:** [May 7, 2024, 4:21am UTC](https://discourse.julialang.org/t/creating-single-entry-matrix/113932 "2024-05-07T04:21:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArcticOwl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arcticowl/32/209060_2.png) [@ArcticOwl](https://discourse.julialang.org/u/ArcticOwl)\
**Post date:** [May 7, 2024, 4:21am UTC](https://discourse.julialang.org/t/creating-single-entry-matrix/113932/1 "2024-05-07T04:21:50Z")

</div>

Hello everyone!

I am new to Julia and I want to create single-entry matrices i.e.,

1. 

```julia
 1.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0

```

1. 

```julia
 0.0 0.0 0.0
 0.0 1.0 0.0
 0.0 0.0 0.0

```

1. 

```julia
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 1.0

```

Can please anyone shed some light on this? I tried using the following code

```julia
matrix1 = zeros(3, 3)
matrix1[1, 1] = 1.0
matrix1

```

```julia
matrix2 = zeros(3, 3)
matrix2[1, 1] = 1.0
matrix2

```

```julia
matrix3 = zeros(3, 3)
matrix3[1, 1] = 1.0
matrix3

```

But I want to do it in a more elegant and sleek way.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [May 7, 2024, 4:42am UTC](https://discourse.julialang.org/t/creating-single-entry-matrix/113932/2 "2024-05-07T04:42:31Z")

</div>

IMO there’s nothing wrong with the way you wrote it, but maybe you prefer something like the following:

```julia
one_hot(rows, cols, i, j) = [(r, c) == (i, j) for r in 1:rows, c in 1:cols]

matrix1 = one_hot(3, 3, 1, 1)
matrix2 = one_hot(3, 3, 2, 2)
matrix3 = one_hot(3, 3, 3, 3)

```

---

<div class="post-metadata">

**Author:** ![RobertGregg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertgregg/32/22105_2.png) [@RobertGregg](https://discourse.julialang.org/u/RobertGregg)\
**Post date:** [May 7, 2024, 5:00am UTC](https://discourse.julialang.org/t/creating-single-entry-matrix/113932/3 "2024-05-07T05:00:53Z")

</div>

Maybe a sparse array if there is only going to be one value?

```julia
using SparseArrays
julia> A = dropzeros!(sparse([1,3], [2,3], [1.0,0.0]))
3×3 SparseMatrixCSC{Float64, Int64} with 1 stored entry:
  ⋅ 1.0 ⋅
  ⋅ ⋅ ⋅
  ⋅ ⋅ ⋅

```

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [May 7, 2024, 5:18am UTC](https://discourse.julialang.org/t/creating-single-entry-matrix/113932/4 "2024-05-07T05:18:29Z")

</div>

`FillArrays` has a `OneElement` type that provides a non-materialized implementation:

```julia
julia> OneElement(1 #= value =#, (2,2) #= index =#, (3,3) #= size =#)
3×3 OneElement{Int64, 2, Tuple{Int64, Int64}, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}:
 ⋅ ⋅ ⋅
 ⋅ 1 ⋅
 ⋅ ⋅ ⋅

```

If you want a matrix from this, you may convert it:

```julia
julia> A = OneElement(1, (2,2), (3,3));

julia> Matrix(A)
3×3 Matrix{Int64}:
 0 0 0
 0 1 0
 0 0 0

```

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [May 7, 2024, 6:48am UTC](https://discourse.julialang.org/t/creating-single-entry-matrix/113932/5 "2024-05-07T06:48:35Z")

</div>

Just based on this approach:

```julia-repl
julia> matrix2 = CartesianIndices((3, 3)) .== CartesianIndex(2, 2)
3×3 BitMatrix:
 0 0 0
 0 1 0
 0 0 0

```

Cartesian indices are nice, just a bit verbose (due to the long names).
