MATLAB中的动态内存分配
我有两个问题:MATLAB中的动态内存分配,matlab,vector,dynamic-memory-allocation,Matlab,Vector,Dynamic Memory Allocation,我有两个问题: 什么是MATLAB与C的等价物? 是吗 如何初始化可用于增量添加对象/结构类型新元素的MALTAB向量 例如,my_vector=zero(1,N)不能用于对象/结构,对吗?在MATLAB中,内存分配是自动完成的。即,向向量添加元素会自动执行realloc x = [ 1 2 3 ]; x(4) = 4; % performs realloc % now x == [1 2 3 4] x(2) = []; % deletes element 2 % now x == [1 3
例如,
my_vector=zero(1,N)
不能用于对象/结构,对吗?在MATLAB中,内存分配是自动完成的。即,向向量添加元素会自动执行realloc
x = [ 1 2 3 ];
x(4) = 4; % performs realloc
% now x == [1 2 3 4]
x(2) = []; % deletes element 2
% now x == [1 3 4]
为了创建对象数组,我在过去使用了repmat
。由于一般情况下需要从一些数据构造对象,因此我发现如果不知道类的其他信息,复制通常是最好的。要创建类CLS
的默认构造对象的2x3x4数组,请使用
x = repmat( CLS(), [ 2 3 4] )
我觉得这比写作更合适
x = CLS();
x(2,3,4) = CLS();
这可能也会工作,但阅读起来很难,如果类没有正确实现,可能会有细微的错误
struct
s也可以使用repmat创建,或者,通过向struct
构造函数提供单元格数组来创建,例如
x = struct( 'a', { 1 2 3}, 'b', { 5 6 7} );
% now x is a 1x3 struct array