'MATLAB - Split a large matrix into submatrices efficiently

I have a large rectangular matrix (like 3000x3000) in MATLAB and I need to split it into several square submatrices (starting from 1x1 and ending at 3000x3000) to do some scientific calculations. a simplified example would be as follows:

01 02 03 04

05 06 07 08

09 10 11 12

13 14 15 16

submatrices:

1x1: [01],[02].... [16]

2x2: [[01,02], [05,06]], [[02,03], [06,07]].... [[11,12],[15,16]]

until

4x4: same as the given matrix.

I have used several for loops but it takes a lot of time and seems not efficient. How can I do that more efficiently?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source