Я думаю, что код ниже может ответить на ваш вопрос. Хитрость заключается в том, чтобы не вызывать какие-либо функции внутри цикла и предварительно распределить все массивы. Смотрите, например http://www.quantiphile.com/2010/10/16/optimizing-matlab-code/ для получения дополнительной информации об ускорении контура. В любом случае, на моей машине ниже ускоренная петля идет в 17 раз быстрее.
% image size
height = 800;
width = 1200;
window = 8;
% histogram bin centers
bin_centers = 0.05:0.1:1;
% here a random image as input
img = rand(height, width);
% verion using accelerated loops (for this to work there cannot be any
% function calls to not built-in functions)
tic
img3 = zeros(window^2, height*width/window^2);
ind = 1;
for i=1:window:height
for j=1:window:width
patch_ = img(i:i+window-1,j:j+window-1);
img3(:,ind) = patch_(:);
ind = ind + 1;
end
end
hist_img3 = hist(img3, bin_centers);
toc
% probably version of user499372 calling hist function within the loop
tic
hist_img4 = zeros(size(hist_img3));
ind = 1;
for i=1:window:height
for j=1:window:width
patch_ = img(i:i+window-1,j:j+window-1);
hist_img4(:,ind) = hist(patch_(:), bin_centers);
ind = ind + 1;
% compute histogram for the patch
end
end
toc
% test the results
all(all(hist_img3==hist_img4))