简体   繁体   English

结构数组导入-内存预分配问题

[英]Array of structures importing - memory prealocation problemMatLab

I have a few .mat files, each of them has an array of structures (of unknown length) called DATA. 我有几个.mat文件,每个文件都有一个称为DATA的结构数组(长度未知)。 I want to import all these structures in a single array, but I don't want to use this code: 我想将所有这些结构导入单个数组中,但是我不想使用此代码:

FileNames = strcat('file',num2str((1:N)''),'.mat'); 
DATATemp = [];

for int = 1:length(FileNames)
    load(FileNames(int,:));
    DATATemp=[DATATemp DATA];
end 

DATA = DATATemp;

because it does not prealocate the memory for the array. 因为它不会为数组预先分配内存。

Are there any clever ways of doing that? 有什么聪明的方法吗?

If the length is short enough that you can over-allocate memory, you can do something like this: Pick an array size that is way larger than what you will ever see, and then trim it back down after you are done. 如果长度足够短以至于您可以过度分配内存,则可以执行以下操作:选择一个比您所看到的更大的数组大小,然后在完成后将其修剪掉。

FileNames = strcat('file',num2str((1:N)''),'.mat'); 
DATATemp = zeros(1e6,1);

idx = 1;
for int = 1:length(FileNames)
    load(FileNames(int,:));
    idx_end = idx + length(DATA) - 1;
    DATATemp(idx:idx_end) = DATA;
    idx = idx_end + 1;
end 

DATA = DATATemp(1:idx_end);

However, if you are talking about a LOT of data, or just want to cover all your bases, a more rigorous solution is to allocate in chunks 但是,如果您谈论的是大量数据,或者只是想覆盖所有基础,则更严格的解决方案是按块分配

FileNames = strcat('file',num2str((1:N)''),'.mat');

CHUNK_SIZE = 1e6; 
DATATemp = zeros(INIT_SIZE,1);

idx = 1;
for int = 1:length(FileNames)
    load(FileNames(int,:));
    idx_end = idx + length(DATA) - 1;
    if idx_end > length(DATATemp)
        DATATemp = [DATATemp zeros(INIT_SIZE ,1);
    DATATemp(idx:idx_end) = DATA;
    idx = idx_end + 1;
end 

DATA = DATATemp(1:idx_end);

Just make sure that your CHUNK_SIZE is significantly larger than the size of a typical individual file. 只要确保您的CHUNK_SIZE明显大于典型单个文件的大小即可。 I picked 1e6 here. 我在这里选了1e6。 That's what I would pick if I was loading ~20 files with an average size of 1e5 each. 如果我要加载〜20个平均大小为1e5的文件,这就是我会选择的。 That way, although I'm still concatenating more space, it's much less often. 这样,尽管我仍在串联更多的空间,但使用频率却要低得多。 This may not be very clever, but I hope it helps. 这可能不是很聪明,但我希望能有所帮助。

Also note that if you are loading files from a network drive that will slow things down immensely. 另请注意,如果您要从网络驱动器加载文件,这将大大降低速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM