# Generate an array of matrices

**URL:** https://discourse.julialang.org/t/generate-an-array-of-matrices/77798
**Category:** New to Julia
**Tags:** arrays
**Created:** [March 12, 2022, 3:46pm UTC](https://discourse.julialang.org/t/generate-an-array-of-matrices/77798 "2022-03-12T15:46:18Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![jisutich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jisutich/32/33342_2.png) [@jisutich](https://discourse.julialang.org/u/jisutich)
#### Post date: [March 12, 2022, 3:46pm UTC](https://discourse.julialang.org/t/generate-an-array-of-matrices/77798/1 "2022-03-12T15:46:18Z")

</div>

Hi I want to generate an array `A` in which each element is a 2\times 2 matrix. And I want to initialize this array such that each element is `[1/2 0; 0 1/2]`. Later I want to replace some element by different matrices like `A[i] = [a b; c d]` and I want to be able to get each element, ,like `temp = A[j]` for further computation. What is the efficient way to do this? Thanks.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [March 12, 2022, 4:00pm UTC](https://discourse.julialang.org/t/generate-an-array-of-matrices/77798/2 "2022-03-12T16:00:48Z")

</div>

Edit: No, `fill` fills the array with addresses to the same matrix, my bad. Here’s how to do it:

```julia
A = [[0.5 0; 0 0.5] for i=1:10]
A[2] .= [1 1;1 0]
B = A[5]

```

---

<div class="post-metadata">

### Author: ![jisutich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jisutich/32/33342_2.png) [@jisutich](https://discourse.julialang.org/u/jisutich)
#### Post date: [March 12, 2022, 4:08pm UTC](https://discourse.julialang.org/t/generate-an-array-of-matrices/77798/3 "2022-03-12T16:08:33Z")

</div>

Yes this works. Thank you!

---

<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: [March 12, 2022, 4:42pm UTC](https://discourse.julialang.org/t/generate-an-array-of-matrices/77798/4 "2022-03-12T16:42:55Z")

</div>

For 2x2 matrices I would use StaticArrays:

```julia
using StaticArrays 
A = fill(SA[0.5 0.0; 0.0 0.5], 10)
A[2] = SA[1.0 1.0;1.0 0.0] 

```

SArrays are immutable, so you cannot use dot-assignment in the last line, but you _can_ use `fill`.

This will be much more efficient than regular arrays.

---

<div class="post-metadata">

### Author: ![jisutich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jisutich/32/33342_2.png) [@jisutich](https://discourse.julialang.org/u/jisutich)
#### Post date: [March 12, 2022, 8:18pm UTC](https://discourse.julialang.org/t/generate-an-array-of-matrices/77798/5 "2022-03-12T20:18:48Z")

</div>

Sorry, I can’t see why `fill` doesn’t work. I tried the previous version `A = fill([1/2 0; 0 1/2],10)` and it gives me an array of matrices. I can also change the elements like `A[1] = [1 2;3 4]`. Could you please explain why `fill` fails? Thank you.

---

<div class="post-metadata">

### Author: ![Paulo\_Jabardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulo_jabardo/32/3196_2.png) [@Paulo\_Jabardo](https://discourse.julialang.org/u/Paulo_Jabardo)
#### Post date: [March 12, 2022, 8:24pm UTC](https://discourse.julialang.org/t/generate-an-array-of-matrices/77798/6 "2022-03-12T20:24:58Z")

</div>

The problem is that every element of the vector `A` refers to the same object.

```julia-repl

julia> A = fill([1/2 0; 0 1/2], 4)
4-element Vector{Matrix{Float64}}:
 [0.5 0.0; 0.0 0.5]
 [0.5 0.0; 0.0 0.5]
 [0.5 0.0; 0.0 0.5]
 [0.5 0.0; 0.0 0.5]

julia> A[1][1,1] = 123
123

julia> A
4-element Vector{Matrix{Float64}}:
 [123.0 0.0; 0.0 0.5]
 [123.0 0.0; 0.0 0.5]
 [123.0 0.0; 0.0 0.5]
 [123.0 0.0; 0.0 0.5]

```

Basically you have the same matrix several times! I don’t think you want that.

---

<div class="post-metadata">

### Author: ![jisutich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jisutich/32/33342_2.png) [@jisutich](https://discourse.julialang.org/u/jisutich)
#### Post date: [March 12, 2022, 8:26pm UTC](https://discourse.julialang.org/t/generate-an-array-of-matrices/77798/7 "2022-03-12T20:26:33Z")

</div>

Oh I see. Thank you!

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [March 12, 2022, 8:36pm UTC](https://discourse.julialang.org/t/generate-an-array-of-matrices/77798/8 "2022-03-12T20:36:40Z")

</div>

I second @DNF’s suggestion. The `fill` issue just described is not a problem with `SMatrix`.

---

<div class="post-metadata">

### Author: ![jisutich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jisutich/32/33342_2.png) [@jisutich](https://discourse.julialang.org/u/jisutich)
#### Post date: [March 12, 2022, 10:20pm UTC](https://discourse.julialang.org/t/generate-an-array-of-matrices/77798/9 "2022-03-12T22:20:40Z")

</div>

Hi guys. I have one more question. Suppose I need to replace one element of the array to be a complex matrix like ` A[i] = [1+im 0;0 1-im]`, then I need to use `[1/2+0*im 0; 0 1/2+0*im]` when I initialize the array, right?

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [March 12, 2022, 11:20pm UTC](https://discourse.julialang.org/t/generate-an-array-of-matrices/77798/10 "2022-03-12T23:20:10Z")

</div>

> [@Elrod](#):
>
> The `fill` issue just described is not a problem with `SMatrix` .

> [@DNF](#):
>
> but you _can_ use `fill`

Just to be clear for those who don’t know, `fill` has the exact same semantics in both cases, it’s just that if the array is immutable (like a `StaticArray`) then there’s no difference between `n` references to the same object and a `n` distinct objects. But if you are doing the same operations with both types (without errors), you should get the same results. Maybe that’s pedantic but to me it feels confusing to describe the behavior as depending on the mutability of the object. That said, I agree `fill` is often more useful when there’s no danger of mutating, which you get with `StaticArray`s.

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [March 12, 2022, 11:21pm UTC](https://discourse.julialang.org/t/generate-an-array-of-matrices/77798/11 "2022-03-12T23:21:30Z")

</div>

Yeah, you need to do that, or you could specify the type when creating the matrices directly, like

```julia
A = [Complex{Float64}[0.5 0; 0 0.5] for i=1:10]

```

---

<div class="post-metadata">

### Author: ![jisutich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jisutich/32/33342_2.png) [@jisutich](https://discourse.julialang.org/u/jisutich)
#### Post date: [March 13, 2022, 3:43am UTC](https://discourse.julialang.org/t/generate-an-array-of-matrices/77798/12 "2022-03-13T03:43:43Z")

</div>

> [@ericphanson](#):
>
> `Comp`

Got it. Thanks

---

<div class="post-metadata">

### Author: ![trilobit](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@trilobit](https://discourse.julialang.org/u/trilobit)
#### Post date: [March 14, 2022, 1:45pm UTC](https://discourse.julialang.org/t/generate-an-array-of-matrices/77798/13 "2022-03-14T13:45:03Z")

</div>

And Now for Something Completely Different 😉  
Generate a normal array and divide it into blocks  
You can index with the normal index, or blockwise  
ie.

 ![Untitled](https://global.discourse-cdn.com/julialang/original/3X/e/b/ebb57c74ca82f59f7c3104c508002ef4b7ea6da7.webp)  
.
