You can obtain the output cell C
from XX
and YY
given in SOL 2 as follows:
% indices of the first rr x cc block
IDX1 = bsxfun(@plus, (1:rr)', ((1:cc)-1)*M);
% offset in indices for each block
offset = (XX(:)-1) + (YY(:)-1)*M;
% indices of each block, the block is indexed with the 3rd dimension
IDX = bsxfun(@plus, IDX1, reshape(offset, [1 1 numel(offset)]));
% convert to cell of blocks
C = mat2cell(Im(IDX), rr, cc, ones(1, numel(XX)));