# List of matrices

**URL:** https://discourse.julialang.org/t/list-of-matrices/110456
**Category:** Performance
**Tags:** matrices
**Created:** [February 20, 2024, 2:38pm UTC](https://discourse.julialang.org/t/list-of-matrices/110456 "2024-02-20T14:38:40Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Enlil50](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/enlil50/32/206539_2.png) [@Enlil50](https://discourse.julialang.org/u/Enlil50)
#### Post date: [February 20, 2024, 2:38pm UTC](https://discourse.julialang.org/t/list-of-matrices/110456/1 "2024-02-20T14:38:40Z")

</div>

Is there something better than this, to hold matrices? (not that i complain, just curious)

```julia
C = Vector{AbstractMatrix{Float64}}(undef, 2)
for i = 1:2
    A = rand(Float64, i*2, i*2)
    display(A)
    C[i] = A
end
display(C)
display(C[1])

```

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [February 20, 2024, 2:44pm UTC](https://discourse.julialang.org/t/list-of-matrices/110456/2 "2024-02-20T14:44:20Z")

</div>

you might want to use an `Array{Float64, 3}`, but a list of matrix is absolutely a reasonable way to do this.

---

<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: [February 20, 2024, 2:46pm UTC](https://discourse.julialang.org/t/list-of-matrices/110456/3 "2024-02-20T14:46:39Z")

</div>

`AbstractMatrix` is an abstract type. It’s better to have concrete types for array elements, if it’s possible. In this case a `Vector{Matrix{Float64}}` would work.

Additionally, creating the list with a comprehension might be easier:

```julia
C = [rand(i*2, i*2) for i in 1:2]

```

---

<div class="post-metadata">

### Author: ![Enlil50](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/enlil50/32/206539_2.png) [@Enlil50](https://discourse.julialang.org/u/Enlil50)
#### Post date: [February 20, 2024, 2:53pm UTC](https://discourse.julialang.org/t/list-of-matrices/110456/4 "2024-02-20T14:53:13Z")

</div>

I already know the dimension of them, but the matrices are not allocated by a simple rand call. Is it possible to give him the dimension information too, or doing so would not give any gain?  
In particular i could just simply allocate the matrix and store a vector of pointers to them, without actually preallocating data inside the matrix holder itself.

More specifically I need a SparseMatrixCSC{Float64, Int64} holder, but i guess that knowing the size of the matrix as if it were dense would not give me any gain. I could compute an algorithm for the nonzeros too, but just a pointer to an already allocated object could be enough to deal with it

---

<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: [February 20, 2024, 2:59pm UTC](https://discourse.julialang.org/t/list-of-matrices/110456/5 "2024-02-20T14:59:06Z")

</div>

> [@Enlil50](#):
>
> I already know the dimension of them

If you mean the **dimension** in the Julia type sense (i.e. vector/matrix/3-dimensional array…) then this is important, as it is part of the type. The actual **size** is not important with Arrays as it is a dynamic quantity.

If the **size** is also known, considering StaticVectors could be an option because they are faster.

---

<div class="post-metadata">

### Author: ![Enlil50](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/enlil50/32/206539_2.png) [@Enlil50](https://discourse.julialang.org/u/Enlil50)
#### Post date: [February 20, 2024, 3:15pm UTC](https://discourse.julialang.org/t/list-of-matrices/110456/6 "2024-02-20T15:15:31Z")

</div>

oh, well, both. I can consider staticarrays for some cases then.  
The little issue could be the actual size for sparsevectors, as they allocate in a different manner and the time spent computing them could not justify the time gained by specifying them in the preallocation. But i have dense matrices too

---

<div class="post-metadata">

### Author: ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)
#### Post date: [February 21, 2024, 7:33am UTC](https://discourse.julialang.org/t/list-of-matrices/110456/7 "2024-02-21T07:33:14Z")

</div>

If your matrices are large enough so that StaticArrays.jl is not worth it then there is probably not terribly much to be gained from trying to specify the size at run time.  
Something like this is fine  
`C = Vector{Matrix{Float64}}(undef, 2)`  
or  
`C = Vector{SparseMatrixCSC{Float64,Int64}}(undef, 2)`

If you don’t know how large the vector of matrices will be:

```julia
C = Vector{Matrix{Float64}}()
for i = 1:2
  A = rand(Float64, i*2, i*2)
  push!(C,A)
end

```

Of course, if you are able to specify your problem via an `Array{Float64, 3}`, (or a sparse version of that) this would require you to know the size of the matrices at the time of the arrays creation. This can be a bit more efficient than a vector of matrices since its contiguous but probably requires you to bend backwards to make it possible. Depending on what you want to do with it it might not be worth the trouble

Edit: I should clarify, if you use a dense array, its will not be so difficult since you can just fill views of the array:

```julia
C = zeros(N1,N2,2)
for i = 1:2
  Aview = @view C[:,:,i]
  rand!(Aview) # or Aview .= myMatrixConstructor(...) or whatever you want
end

```
