# How to define a Matrix{UInt8} exhibiting chess board patterns

**URL:** <https://discourse.julialang.org/t/how-to-define-a-matrix-uint8-exhibiting-chess-board-patterns/100760>\
**Category:** General Usage\
**Tags:** array\
**Created:** [June 23, 2023, 8:50pm UTC](https://discourse.julialang.org/t/how-to-define-a-matrix-uint8-exhibiting-chess-board-patterns/100760 "2023-06-23T20:50:10Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [June 23, 2023, 8:50pm UTC](https://discourse.julialang.org/t/how-to-define-a-matrix-uint8-exhibiting-chess-board-patterns/100760/1 "2023-06-23T20:50:10Z")

</div>

I need a `Matrix{UInt8}`, P, of 0 and 1 which exhibits chess board patterns  
of submatrices A of size(A)=(5,5).  
I defined it as follows:

```julia
P=zeros(UInt8, 100,100)
A= fill(UInt8(1), (5,5))
for k in 0:2:18
    for j in 0:2:18
        P[5k+1:5k+5, 5j+1:5j+5] .=A
    end    
end
for k in 1:2:19
    for j in 1:2:19
        P[5k+1:5k+5, 5j+1:5j+5] .=A
    end    
end

```

Is there a simpler method to define P?

---

<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:** [June 23, 2023, 9:07pm UTC](https://discourse.julialang.org/t/how-to-define-a-matrix-uint8-exhibiting-chess-board-patterns/100760/2 "2023-06-23T21:07:36Z")

</div>

Simpler? Maybe, you can for instance just replace `A` with `UInt8(1)`. Shorter? Absolutely:

```julia
P = [UInt8(mod(div(i, 5) + div(j, 5), 2)) for i in 1:100, j in 1:100]

```

(I’m writing this on my phone after a quite wet midsummer celebration, so I don’t guarantee it works. If you want me to test it, ping me tomorrow)

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [June 23, 2023, 9:38pm UTC](https://discourse.julialang.org/t/how-to-define-a-matrix-uint8-exhibiting-chess-board-patterns/100760/3 "2023-06-23T21:38:32Z")

</div>

With a slight modification P is defined as:

```julia
P=[UInt8(mod(div(i+4, 5)+div(j+4, 5), 2)) for i=1:100, j=1:100]

```

Thank you!!!

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [June 24, 2023, 11:35am UTC](https://discourse.julialang.org/t/how-to-define-a-matrix-uint8-exhibiting-chess-board-patterns/100760/4 "2023-06-24T11:35:43Z")

</div>

For this specific example there is also the `repeat` function.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 24, 2023, 1:22pm UTC](https://discourse.julialang.org/t/how-to-define-a-matrix-uint8-exhibiting-chess-board-patterns/100760/5 "2023-06-24T13:22:17Z")

</div>

```julia

Z(s)=zeros(Int, s,s)
O(s)=ones(Int, s,s)
blockm(s,m,n)=@view hvcat(ntuple(_->n ÷ 2+n%2 ,m÷2+m%2),fill([Z(s) O(s) ; O(s) Z(s)], n ÷ 2+n%2 ,m÷ 2+m%2)...)[1:s*m,1:s*n]

julia> blockm(3,4,3)
12×9 view(::Matrix{Int64}, 1:12, 1:9) with eltype Int64:
 0 0 0 1 1 1 0 0 0
 0 0 0 1 1 1 0 0 0
 0 0 0 1 1 1 0 0 0
 1 1 1 0 0 0 1 1 1
 1 1 1 0 0 0 1 1 1
 1 1 1 0 0 0 1 1 1
 0 0 0 1 1 1 0 0 0
 0 0 0 1 1 1 0 0 0
 0 0 0 1 1 1 0 0 0
 1 1 1 0 0 0 1 1 1
 1 1 1 0 0 0 1 1 1
 1 1 1 0 0 0 1 1 1

```

in the solution expression the indices should start from 0 not from 1. isn’t it?

> [@gustaphe](#):
>
> `for i in 1:100, j in 1:100]`

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [June 24, 2023, 1:51pm UTC](https://discourse.julialang.org/t/how-to-define-a-matrix-uint8-exhibiting-chess-board-patterns/100760/6 "2023-06-24T13:51:28Z")

</div>

Yes, but as I mentioned above I modified @gustaphe’s i and j.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 24, 2023, 1:52pm UTC](https://discourse.julialang.org/t/how-to-define-a-matrix-uint8-exhibiting-chess-board-patterns/100760/7 "2023-06-24T13:52:33Z")

</div>

like this or is there a more direct way?

```julia
blockm(s,m,n) = repeat([0 1; 1 0],inner=(s,s), outer=((m÷2)+(m%2),(n÷2)+(n%2)))[1:m*s,1:n*s]

```

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [June 24, 2023, 3:58pm UTC](https://discourse.julialang.org/t/how-to-define-a-matrix-uint8-exhibiting-chess-board-patterns/100760/8 "2023-06-24T15:58:39Z")

</div>

Though not memory efficient, you could do it easily with the [Kronecker Product](https://en.wikipedia.org/wiki/Kronecker_product).  
Create a regular chess pattern and multiply it by a matrix of ones of the size of the block you want.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 24, 2023, 4:41pm UTC](https://discourse.julialang.org/t/how-to-define-a-matrix-uint8-exhibiting-chess-board-patterns/100760/9 "2023-06-24T16:41:01Z")

</div>

simil kroneker

```julia
O(s)=ones(Bool,s,s)
mb(n,m)=[(i+j)%2==0 for i in 1:n, j in 1:m]

mb(m,n)=reduce(hcat,accumulate(xor, fill(true,m),init=i) for i in accumulate(xor,fill(true,n)))

blockm(s,m,n)=hvcat(ntuple(_->n,m),mb(n,m).*[O(s)]...)

```

```julia
O(s)=ones(Bool,s,s
b(x) = accumulate(xor,fill(true,x))
blockm(s,m,n) = hvcat(ntuple(_->n,m),xor.(b(n), b(m)').*[O(s)]...)

```

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [June 24, 2023, 6:35pm UTC](https://discourse.julialang.org/t/how-to-define-a-matrix-uint8-exhibiting-chess-board-patterns/100760/10 "2023-06-24T18:35:26Z")

</div>

![helic2catenL20fps](https://global.discourse-cdn.com/julialang/original/3X/1/9/19de27839e3f01c1d67bdf550cc6984a738f64b8.gif)

---

<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:** [June 24, 2023, 6:55pm UTC](https://discourse.julialang.org/t/how-to-define-a-matrix-uint8-exhibiting-chess-board-patterns/100760/11 "2023-06-24T18:55:40Z")

</div>

Simple enough to fix, just add 1 to the argument of `mod`. I thought demonstrating the principle was meaningful even without spending too long parsing the example.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 24, 2023, 7:54pm UTC](https://discourse.julialang.org/t/how-to-define-a-matrix-uint8-exhibiting-chess-board-patterns/100760/12 "2023-06-24T19:54:02Z")

</div>

It wasn’t meant to be a critical remark, just a request for clarification to make sure I understood the problem.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 24, 2023, 7:58pm UTC](https://discourse.julialang.org/t/how-to-define-a-matrix-uint8-exhibiting-chess-board-patterns/100760/13 "2023-06-24T19:58:27Z")

</div>

is the table the initial condition of this transformation?  
what is the code that produces this animation?

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [June 24, 2023, 8:21pm UTC](https://discourse.julialang.org/t/how-to-define-a-matrix-uint8-exhibiting-chess-board-patterns/100760/14 "2023-06-24T20:21:21Z")

</div>

The posted animation contains 140 frames. This is the code for the first 35 frames:

```julia
using Plots
plotlyjs();

function intermsurf(;r=2, α=0.0)   
    n = 100
    v = range(-r, r; length = n)
    u = range(-π, π; length = n)
    x = @. cos(α)*sinh(v)*sin(u)' + sin(α)*cosh(v)*cos(u)'
    y = @. sin(α)*cosh(v)*sin(u)' - cos(α)*sinh(v)*cos.(u)' 
    z = sin(α)*collect(v)*ones(size(u))' +cos(α)*ones(size(v))*collect(u)'
    return x, y, z
end
bw = cgrad([:black, RGB{Float64}(0.9, 0.9, 0.9)])
surfpatterns = [UInt8(mod(div(i+4,5)+div(j+4,5), 2)) for i=1:100, j=1:100];

a=vcat(collect(range(0, π/2; length=30)), fill(π/2, 5))
@gif for α∈a
     x, y, z= intermsurf(; α=α) 
     Plots.surface(x, y, z, fill_z = surfpatterns, color=palette(bw, 2), 
              colorbar=false, showaxis=false,  
              size=(450, 400), camera=(45, 20))
end

```
