performance - 在 MATLAB 中将函数应用于给定大小的每个子矩阵的有效方法

标签 performance matlab

在 MATLAB 中,我想对给定大小的矩阵的每个子矩阵应用一个函数。

我当前的代码可以运行,但速度很慢。

%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

非常感谢任何建议!

最佳答案

如果你有图像处理工具箱,你可以使用blockprocM[h w] 滑动窗口上应用 f:

out = blockproc(M, [h w], f);

关于performance - 在 MATLAB 中将函数应用于给定大小的每个子矩阵的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66662475/

相关文章:

c++ - 尝试访问结构时 Matlab 编码器构建错误

algorithm - MATLAB 函数可以将数学函数作为输入吗?

php - 如何提高 MySQL INSERT 性能?

Java多线程应用程序只使用一个核心

java - 字节码中 Switch 语句的表示和效率?

matlab - 查找第一个元素最接近的行

matlab - 如何忽略matlab中的输出变量?

arrays - 从矩阵matlab中选取最有值(value)的元素

c# - 快速功能可根据键返回字符串,反之亦然

performance - Opengl glScalef 重置性能