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

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

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

As for the first, if you look at

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

you will find that it takes you to a method for sparse concatenation. The magic happens here:

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

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

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.

1 Like