In MATLAB, I want to apply a function to each submatrix of the given size of a matrix.
My current code works but is very slow.
%inputs
% f: function to apply to submatrices
% M: main matrix
% h: height of submatrices
% w: width of submatrices
%output
% out: matrix of results of applying f to submatrices of M
function out = metricmap(f,M,h,w)
[r,c] = size(M);
h = h-1;w = w-1; %to make these values deltas
out = zeros([r,c]-[h,w]);
for i = 1:(r - h)
for j = 1:(c - w)
subM = M(i:(i+h),j:(j+w));
out(i,j) = f(subM);
end
end
end
Any suggestions are greatly appreciated!