아래 코드는 귀하의 질문에 대한 답변입니다. 트릭은 루프 내에서 함수를 호출하지 않고 모든 배열을 사전 할당하는 것입니다. 예 : 루프 가속에 대한 자세한 내용은 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))