Matlab内存不足 - 如何对矩阵元素进行就地操作?

Sti*_*fel 5 matlab matrix out-of-memory in-place

我正在将一个相当大的矩阵加载到Matlab中.加载这个矩阵已经将Matlab推向极限 - 但它很合适.

然后我执行以下操作,我得到一个内存不足的错误.

data( :, 2:2:end, :, : ) = - data( :, 2:2:end, :, : );

Matlab是否为此操作分配了一个新矩阵?我认为这个操作不需要额外的内存.如何强制Matlab提高效率呢?

奖金问题:

'data = permute(data,[1 2 3 4 5 12 8 7 6 9 10 11]);'

matlab可以就地执行此操作吗?

Edr*_*ric 4

有一些限制(除了约翰引用的 Loren 块中的限制):

  • 您的代码必须在函数内运行
  • 您不能有其他“data”别名

“别名”的事情很重要,但可能很难正确处理。MATLAB 使用写时复制,这意味着当您调用函数时,您传递的参数不会立即复制,但如果您在函数内修改它,则可能会复制它。例如,考虑

x = rand(100);
y = myfcn(x);
% with myfcn.m containing:
function out = myfcn(in)
  in(1) = 3;
  out = in * 2;
end
Run Code Online (Sandbox Code Playgroud)

在这种情况下,变量x将传递给myfcn. MATLAB 具有值语义,因此对输入参数的任何修改都in不能在调用工作区中看到。因此,第一行myfcn导致参数in成为 的副本x而不仅仅是它的别名。try考虑一下/会发生什么catch- 这可能是一个就地杀手,因为 MATLAB 必须能够在出错时保留值。在下面的:

% consider this function
function myouterfcn()
  x = rand(100);
  x = myfcn(x);
end
% with myfcn.m containing
function arg = myfcn( arg )
  arg = -arg;
end
Run Code Online (Sandbox Code Playgroud)

那么,应该x对in进行就地优化myouterfcn。但以下情况不能:

% consider this function
function myouterfcn()
  x = rand(100);
  x = myfcn(x);
end
% with myfcn.m containing
function arg = myfcn( arg )
  try
    arg = -arg;
  catch E
    disp( 'Oops.' );
  end
end
Run Code Online (Sandbox Code Playgroud)

希望这些信息中的一些有所帮助...