# \`\[I(2) rand(2,100)\]\` is sparse?

**URL:** <https://discourse.julialang.org/t/i-2-rand-2-100-is-sparse/31241>\
**Category:** General Usage\
**Created:** [November 18, 2019, 10:18pm UTC](https://discourse.julialang.org/t/i-2-rand-2-100-is-sparse/31241 "2019-11-18T22:18:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [November 18, 2019, 10:18pm UTC](https://discourse.julialang.org/t/i-2-rand-2-100-is-sparse/31241/1 "2019-11-18T22:18:05Z")

</div>

`[I(2) rand(2,100)]` gives a sparse matrix. May I ask why?

---

<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:** [November 19, 2019, 7:01am UTC](https://discourse.julialang.org/t/i-2-rand-2-100-is-sparse/31241/2 "2019-11-19T07:01:35Z")

</div>

I am not sure if you are asking about implementation details or design choices.

As for the first, if you look at

```julia
@edit [I(2) rand(2,100)]

```

you will find that it takes you to a method for sparse concatenation. The magic happens here:  
[https://github.com/JuliaLang/julia/blob/1e39c69a550c9be945db2ccf1ab6ded02c404ec4/stdlib/SparseArrays/src/sparsevector.jl#L1065](https://github.com/JuliaLang/julia/blob/1e39c69a550c9be945db2ccf1ab6ded02c404ec4/stdlib/SparseArrays/src/sparsevector.jl#L1065)

As for the design choice: yes, for your case it is dense, but in other cases with a large `I` it would be sparse, and it is hard to predict the best solution from the type. Perhaps you can clarify with

```julia
[Matrix(I(2)) rand(2,100)]

```

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [November 19, 2019, 8:03am UTC](https://discourse.julialang.org/t/i-2-rand-2-100-is-sparse/31241/3 "2019-11-19T08:03:02Z")

</div>

Thanks, I was interested in both aspects.

In terms of the design choice, I was a first surprised since `[I rand(2,100)]` gives a dense matrix.

Upon closer study, I realise that `I(2)` is just `Diagonal([1,1])` which explains the origin of the `[I(2) rand(2,100)]` behaviour.

Well, I don’t need `I(n)` other than in some Kronecker products.
