如何在MATLAB中预分配非数值向量?

Dan*_*ton 5 matlab pre-allocation matlab-struct

我经常发现自己在做这样的事情:

unprocessedData = fetchData();  % returns a vector of structs or objects
processedData = [];             % will be full of structs or objects

for dataIdx = 1 : length(unprocessedData) 
    processedDatum = process(unprocessedData(dataIdx));
    processedData = [processedData; processedDatum];
end
Run Code Online (Sandbox Code Playgroud)

尽管功能强大,但并不是最佳选择- processedData向量在循环内增长。甚至mlint警告我,我应该考虑预先分配速度。

如果数据是的向量int8,我可以这样做:

% preallocate processed data array to prevent growth in loop
processedData = zeros(length(unprocessedData), 1, 'int8');
Run Code Online (Sandbox Code Playgroud)

并修改循环以填充向量插槽,而不是连接起来。

有没有一种方法可以预分配向量,以便随后可以容纳结构或对象?


更新:Azim的回答启发,我只是颠倒了循环顺序。首先处理最后一个元素将在第一次命中时强制整个向量进行预分配,因为调试器确认:

unprocessedData = fetchData();

% note that processedData isn't declared outside the loop - this breaks 
% it if it'll later hold non-numeric data. Instead we exploit matlab's 
% odd scope rules which mean that processedData will outlive the loop
% inside which it is first referenced: 

for dataIdx = length(unprocessedData) : -1 : 1 
    processedData(dataIdx) = process(unprocessedData(dataIdx));
end
Run Code Online (Sandbox Code Playgroud)

这要求process()自MATLAB 返回的所有对象都必须具有有效的零参数构造函数,因为MATLAB会processedData在第一次写入时使用实际对象对其进行初始化。

mlint 仍然抱怨可能的数组增长,但是我认为这是因为它无法识别反向循环迭代...

gno*_*ice 5

除了Azim的答案,另一种方法是使用repmat

% Make a single structure element:
processedData = struct('field1',[],'field2',[]);
% Make an object:
processedData = object_constructor(...);
% Replicate data:
processedData = repmat(processedData,1,nElements);
Run Code Online (Sandbox Code Playgroud)

nElements结构或对象数组中将具有的元素数在哪里。

注意:如果您要制作的对象是从handle类派生的,则您将不会复制对象本身,只需处理对其的引用即可。根据您的实现,您可能必须调用对象构造方法的nElements时间。


Azi*_*zim 3

由于您知道结构的字段processedData并且知道其长度,因此一种方法如下:

unprocessedData = fetchData();
processedData = struct('field1', [], ...
                       'field2', []) % create the processed data struct
processedData(length(unprocessedData)) = processedData(1); % create an array with the required length
for dataIdx = 1:length(unprocessedData)
    processedData(dataIdx) = process(unprocessedData(dataIdx));
end
Run Code Online (Sandbox Code Playgroud)

这假设该process函数返回一个具有与 相同字段的结构体processedData