Matlab的内存问题:.mat中的更新变量 [英] Memory issue with Matlab: update variable in .mat
问题描述
我正在Matlab中处理非常计算量大的代码.它要求使用优化技术,并使用很大的矩阵进行长时间的计算.
I am working with a very computational expansive code in Matlab. It requires the usage of optimisation techniques and long computations using very big matrixes.
我遇到了以下问题:即使代码正确运行,在代码要求的迭代结束时,Matlab也不会存储我拥有的最大单元格数组.我猜这是由于我的代码或计算机中的某些内存效率低下(功能可能不够强大).但是,我遵循了Matlab文档中的所有一般建议,但仍然无法正常工作.
I am having the following issue: even if the code run correctly, at the end of the iterations required by the code, Matlab is not storing the biggest cell arrays that I have. I guess that it is due to some memory inefficiency in my code or with my computer (which is probably not sufficiently powerful). However, I followed all the general suggestions in the Matlab documentation and it is still not working.
使用evalc,我设法为代码的每次迭代保存一个不同的变量,以便在循环结束时重新创建原始矩阵.但是,使用:
Using evalc, I managed to save a different variable for each iteration of the code, in order to re-create the original matrix at the end of the loop. However, using:
- evalc(strcat('var_',mat2str(i),'= varTmp'));
- evalc(strcat('save(var_',mat2str(i),'-append)'));
- clear var *
..以这种方式工作,但是它仍然很慢并且不是很干净整洁".
.. in this way it is working, but it still slow and not very "clean and tidy".
是否有一种更好的方法(考虑到我必须对具有不同名称和维数的几个变量执行相同的操作)还是在更新.mat文件中的单元格数组以添加列的方法? (行或其他)而不加载它?
Is there a way to do the same thing in a better way (consider that I have to do the same thing for several variables with different names and dimensions) or i.e. to update a cell array in a .mat file adding a column (row or whatever) without loading it?
谢谢
推荐答案
使用matfile
,它使您可以编写和读取Mat文件的一部分,而无需将其读取到内存中.一个小示范:
Use matfile
which allows you writing and reading parts of a mat file without reading it into the memory. A small demonstration:
%initialize matfile
data=matfile('example.mat','writable',true)
n=10
%preallocate cell
data.list=cell(n,1)
for ix=1:n
%do some stuff
var=foo(n)
%store the results
data.list(3,1)={var}
end
行data.list(3,1)={var}
看起来有些奇怪,因为matfile
在索引时有一些限制,但是它的含义"是data.list{3}=var
.
The line data.list(3,1)={var}
looks a little odd because matfile
has some limitations when indexing, but it's "meaning" is data.list{3}=var
.
这篇关于Matlab的内存问题:.mat中的更新变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!