MATLAB: Reshape a photo to 8*8 blocks

cat

Hello,
I would like to reshape a photo into 8*8 blocks and have a new matrix with size of
In other words, I am searching for a matlab function that works the same as the code below, but with higher speed:
for i=1:8:size(img,1)
for j=1:8:size(img,2)
tmp = cat(3,tmp,img(i:i+7, j:j+7));
end
end

Best Answer

Use mat2cell
[m,n] = size(img);
im = 8*ones(1,m/8);
in = 8*ones(1,n/8);
img1 = mat2cell(img,im,in);
img2 = cat(3,img1{:});