1

当有大量矩阵时,我对 Matlab 处理前向乘法矩阵的方式有疑问。

作为一个简化的例子,我想做 x(ii + 1) = y(ii) * x(ii)。但是,当矩阵的维度很大并且数量很多时,内存需求会呈指数增长,超出了应有的范围。

这是一些示例代码,根据 matlab 内存分析器,它使用 1e-07TB 的 RAM,虽然不正确,但此循环通常占用 256GB 的 RAM。

function forward_test()
    K = 250; 
    dim = 1024;
    U = cell(K,1);

    for ii = 1 : K 
        U{ii} = rand(dim,dim) + (1i*rand(dim,dim));
    end

    r_tmp = cell(K + 1, 1); r_tmp{1} = eye(dim);
    for k = 1 : K 
        r_tmp{k+1} = U{k} * r_tmp{k}; % <--- this line is the issue
    end

    % convert the matrix to a 3D array, I'm certian there's a better way of doing this, but this is quick and dirty
    u_test = zeros(dim, dim, K);
    for ii = 1 : K
        u_test(:,:,ii) = U{ii};
    end

    % test the same operation on a 3D array, rather than a set of 2D maricies. 
    tmp2 = zeros(dim, dim, K + 1); tmp2(:,:,1) = eye(dim);
    for ii = 1 : K
        tmp2(:,:, ii + 1) =  U{ii} * tmp2(:,:,ii);
    end 


    for ii = 1 : numel(r_tmp)
        if ~(all(all(r_tmp{ii} == tmp2(:,:,ii))))
            error('Result missmatch!');
        end
    end
end

要检查性能运行:

profile off
profile -memory on
forward_test
profreport
4

0 回答 0