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

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

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]])

Input:

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

Output:

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:

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

Output:

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

Input:

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

Output:

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

Input:

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

Output:

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

Ask the REPL about @views, etc.

julia> ?
help?> "subarray"
help?> view
help?> @view
help?> @views

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

1 Like

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