# Dividing matrix into sections

**URL:** https://discourse.julialang.org/t/dividing-matrix-into-sections/10428
**Category:** Performance
**Created:** [April 19, 2018, 7:10am UTC](https://discourse.julialang.org/t/dividing-matrix-into-sections/10428 "2018-04-19T07:10:38Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)
#### Post date: [April 19, 2018, 7:10am UTC](https://discourse.julialang.org/t/dividing-matrix-into-sections/10428/1 "2018-04-19T07:10:38Z")

</div>

How to divide a matrix like _m_ x _n_ or an Array like _m_ x _n_ x 1 into topographic sections? Imagine dividing a square into smaller squares.  
And operating individually on those sections.  
Edit: **The actual problem is that I want to know the indices of each element of each section the way they are stored in the parent matrix**

---

<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: [April 19, 2018, 7:31am UTC](https://discourse.julialang.org/t/dividing-matrix-into-sections/10428/2 "2018-04-19T07:31:52Z")

</div>

Maybe I am missing something, but possibly with indexing and views? Eg

```julia
julia> A = zeros(3, 3);

julia> Av = @view A[1:2, 1:2];

julia> Av .= ones(2, 2);

julia> A
3×3 Array{Float64,2}:
 1.0 1.0 0.0
 1.0 1.0 0.0
 0.0 0.0 0.0

```

---

<div class="post-metadata">

### Author: ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)
#### Post date: [April 19, 2018, 7:40am UTC](https://discourse.julialang.org/t/dividing-matrix-into-sections/10428/3 "2018-04-19T07:40:20Z")

</div>

But how will we do this for the entire matrix? Say you wish to divide it into 6 squares.

---

<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: [April 19, 2018, 8:06am UTC](https://discourse.julialang.org/t/dividing-matrix-into-sections/10428/4 "2018-04-19T08:06:08Z")

</div>

You specify the division and map it to views. (If you have a concrete example of what you want, I could help with code).

---

<div class="post-metadata">

### Author: ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)
#### Post date: [April 19, 2018, 8:43am UTC](https://discourse.julialang.org/t/dividing-matrix-into-sections/10428/5 "2018-04-19T08:43:31Z")

</div>

```julia
a = rand(1:100, 25, 25, 1)
i, j, one = size(a)
for m in 1:i, for n in 1:j
display(a[max(1, m-4):min(25, m+4), max(1, n-4):min(25, n+4)])
end

```

---

<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: [April 19, 2018, 8:55am UTC](https://discourse.julialang.org/t/dividing-matrix-into-sections/10428/6 "2018-04-19T08:55:09Z")

</div>

```julia
a = rand(1:100, 25, 25, 1)
i, j, _ = size(a) # replace `one`, don't overwrite that function
V = [@view(a[max(1, m-4):min(25, m+4), max(1, n-4):min(25, n+4), :]) # added a :
     for m in 1:i for n in 1:j] # note nesting order, is this what you want?

```

This gives you a vector of views, you can modify the comprehension to make a matrix if that’s what you need.

---

<div class="post-metadata">

### Author: ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)
#### Post date: [April 19, 2018, 9:12am UTC](https://discourse.julialang.org/t/dividing-matrix-into-sections/10428/7 "2018-04-19T09:12:11Z")

</div>

That’s not what I want. It’s just a workaround to store views which I was aware of. I was looking for a function to divide a matrix into mostly equal squares so that I can run a function over those individual sections.  
**But now I realize that the actual problem is that I still want to somehow know the indices of each element of each section the way they are stored in the parent matrix**

---

<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: [April 19, 2018, 9:17am UTC](https://discourse.julialang.org/t/dividing-matrix-into-sections/10428/8 "2018-04-19T09:17:04Z")

</div>

Well, there is `Base.parent` and `Base.parentindexes`. I don’t know the details of your calculation, but this may be a very roundabout way of going about it.

---

<div class="post-metadata">

### Author: ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)
#### Post date: [April 19, 2018, 9:39am UTC](https://discourse.julialang.org/t/dividing-matrix-into-sections/10428/9 "2018-04-19T09:39:14Z")

</div>

Take a look at [GitHub - JuliaArrays/TiledIteration.jl: Julia package to facilitate writing mulithreaded, multidimensional, cache-efficient code](https://github.com/JuliaArrays/TiledIteration.jl)

---

<div class="post-metadata">

### Author: ![abshej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abshej/32/3216_2.png) [@abshej](https://discourse.julialang.org/u/abshej)
#### Post date: [April 19, 2018, 9:43am UTC](https://discourse.julialang.org/t/dividing-matrix-into-sections/10428/10 "2018-04-19T09:43:03Z")

</div>

Thanks a lot.

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [April 20, 2018, 8:34am UTC](https://discourse.julialang.org/t/dividing-matrix-into-sections/10428/11 "2018-04-20T08:34:45Z")

</div>

Or alternatively [https://github.com/JuliaImages/ImageFiltering.jl](https://github.com/JuliaImages/ImageFiltering.jl) which are general array operations, not just for images.

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [April 20, 2018, 11:25pm UTC](https://discourse.julialang.org/t/dividing-matrix-into-sections/10428/12 "2018-04-20T23:25:30Z")

</div>

For square matrices of size n\times n, for example, this can be easy:

```julia

n = 1024
bSize = 16
A = rand(n,n)
for j = 1:bSize:n, i = 1:bSize:n
	tempArray = @view A[i:i+bSize-1,j:j+bSize-1]
	# do calculations on tempArray 
end 

```

For general rectangular matrices m\times n, you need a `row_bSize` and another `col_bSize`, such that `row_bSize` divides m and `col_bSize` divides n but the idea is the same. If the blocks need to be squares, then `row_bSize = col_bSize` and m or n are divisible my either of them.

---

<div class="post-metadata">

### Author: ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)
#### Post date: [April 21, 2018, 6:01am UTC](https://discourse.julialang.org/t/dividing-matrix-into-sections/10428/13 "2018-04-21T06:01:55Z")

</div>

Or [https://github.com/JuliaArrays/BlockArrays.jl](https://github.com/JuliaArrays/BlockArrays.jl)
