有什么方法可以分配矩阵视图,而不是像在 NumPy 中那样在 Matlab 中复制矩阵本身?

Is there any way to assign matrix views instead of copying matrices themselves in Matlab, like in NumPy?

A = B(1, :)

..将B的第一行复制到A.

有没有办法创建矩阵视图对象,比如

frB = view(B(1, :))

能够引用矩阵的视图?此外,这将使它成为

B(1, 3) = 123123;     % set B(1, 3) to 123123 for illustration purposes
frB(3) = 9999;        % set B(1, 3) to 9999
disp(B(1, 3));        % prints 9999

请参阅此 NumPy 示例,它正是这样做的: http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.view.html

您可以在 Matlab 中使用指针指向同一个矩阵,而无需进行复制。这是一个基于 Using pointers in Matlab

代码的简单示例

首先你定义一个class继承自handleclass,也就是Matlab的指针class。 class 属性将存储您的矩阵。

classdef HandleObject < handle
   properties
      Object=[]; % This will be your matrix
   end

   methods
      function obj=HandleObject(receivedObject) %This is the constructor
         obj.Object=receivedObject;
      end
   end
end

要声明矩阵和矩阵视图,请执行以下操作

M = HandleObject(ones(5,5)); %The constructor passes the matrix to the Object property
M_view = M; %M_view is a copy of the pointer

M.Object(1,1) = 5; %Change the matrix by changing the Object property 

display(M_view.Object(1,1)) %This should display 5

您可以向 HandleObject 添加更多功能以对应您想要的视图。