# How to split a matrix in a submatrix of given ranges?

**URL:** <https://discourse.julialang.org/t/how-to-split-a-matrix-in-a-submatrix-of-given-ranges/66642>\
**Category:** General Usage\
**Tags:** question, linearalgebra\
**Created:** [August 18, 2021, 11:20pm UTC](https://discourse.julialang.org/t/how-to-split-a-matrix-in-a-submatrix-of-given-ranges/66642 "2021-08-18T23:20:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![karalis1](https://avatars.discourse-cdn.com/v4/letter/k/aeb1de/32.png) [@karalis1](https://discourse.julialang.org/u/karalis1)\
**Post date:** [August 18, 2021, 11:20pm UTC](https://discourse.julialang.org/t/how-to-split-a-matrix-in-a-submatrix-of-given-ranges/66642/1 "2021-08-18T23:20:26Z")

</div>

Hi all, I’m trying to implement an algorithm but I can’t figure it out how to split a matrix in a submatrix given some ranges

 ![Schermata 2021-08-19 alle 01.32.15](https://global.discourse-cdn.com/julialang/original/3X/d/e/dedde4474c4e878c5d1ef6bad1fff0513a5107c9.png)

I have tried with views and comprehension but didn’t succed to obtain the given ranges. This is my input which is from Julia SuiteSparse Graph Blas, built on top of Julia data structures

matrix = GBMatrix([[0, 1, 1, 0, 0] [1, 0, 1, 1, 1] [1, 1, 0, 0, 1] [0, 1, 0, 0, 1] [0, 1, 1, 1, 0]])

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 19, 2021, 12:16am UTC](https://discourse.julialang.org/t/how-to-split-a-matrix-in-a-submatrix-of-given-ranges/66642/2 "2021-08-19T00:16:41Z")

</div>

Input:

```julia
A = 10(1:9) .+ (1:9)'

```

Output:

```julia
9×9 Matrix{Int64}:
 11 12 13 14 15 16 17 18 19
 21 22 23 24 25 26 27 28 29
 31 32 33 34 35 36 37 38 39
 41 42 43 44 45 46 47 48 49
 51 52 53 54 55 56 57 58 59
 61 62 63 64 65 66 67 68 69
 71 72 73 74 75 76 77 78 79
 81 82 83 84 85 86 87 88 89
 91 92 93 94 95 96 97 98 99

```

Input:

```julia
i = 4
@views A00 = A[begin:i-1, begin:i-1]

```

Output:

```julia
3×3 view(::Matrix{Int64}, 1:3, 1:3) with eltype Int64:
 11 12 13
 21 22 23
 31 32 33

```

Input:

```julia
@views a10 = A[begin:i-1, i:i]

```

Output:

```julia
3×1 view(::Matrix{Int64}, 1:3, 4:4) with eltype Int64:
 14
 24
 34

```

Input:

```julia
@views A02 = A[begin:i-1, i+1:end]

```

Output:

```julia
3×5 view(::Matrix{Int64}, 1:3, 5:9) with eltype Int64:
 15 16 17 18 19
 25 26 27 28 29
 35 36 37 38 39

```

Continued with [https://github.com/genkuroki/public/blob/main/0016/How%20to%20split%20a%20matrix.ipynb](https://github.com/genkuroki/public/blob/main/0016/How%20to%20split%20a%20matrix.ipynb)

Ask the REPL about `@views`, etc.

```julia
julia> ?
help?> "subarray"

```

```julia
help?> view

```

```julia
help?> @view

```

```julia
help?> @views

```

If you ask the REPL every time, your Julia life will be more comfortable. 😉

---

<div class="post-metadata">

**Author:** ![karalis1](https://avatars.discourse-cdn.com/v4/letter/k/aeb1de/32.png) [@karalis1](https://discourse.julialang.org/u/karalis1)\
**Post date:** [August 19, 2021, 1:48pm UTC](https://discourse.julialang.org/t/how-to-split-a-matrix-in-a-submatrix-of-given-ranges/66642/3 "2021-08-19T13:48:00Z")

</div>

Thanks a lot for your answer! I finally managed to implement it!
