Matlab预分配,猜是一个大矩阵还是一个小矩阵? [英] Matlab Preallocation, guess a large matrix or a small one?

查看:144
本文介绍了Matlab预分配,猜是一个大矩阵还是一个小矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此问题,我应该尝试使用Preallocation是Matlab.

According to this question, I should try to use Preallocation is Matlab.

现在,我无法计算要预分配的矩阵的确切大小.我可以猜出它的大小.

Now I have a situation that I cannot calculate the exact size of the matrix to preallocate. I can guess the size.

假设矩阵的实际大小为100,但我不知道.嘘

suppose the actual size of the matrix is 100, but I don't know it. Sh

哪种情况更有效:

  1. 我应该奢侈吗?我猜想矩阵很大,最后我删除了多余的行.
  2. 我应该小气吗?我猜尺寸很小,如果错了,我添加新行.

谢谢.

推荐答案

在我看来,答案比@natan描绘的要复杂一些. 我认为他的回答没有考虑两个因素:

To my opinion, the answer is a bit more complex than portrayed by @natan. I think there are two factors his answer does not take into account:

  1. 可能必要的内存副本:当您低估了矩阵大小并重新分配它时,应将其所有旧值复制到新分配的位置.

  1. Possible necessary copies of memory: when you under-estimate a matrix size and you re-allocate it, all its old values should be copied to the new allocated location.

内存块的连续性:有时Matlab能够在旧矩阵的末尾连续分配新的内存.原则上,在这种情况下,不需要将旧值复制到新位置-因为它与旧值相同,只是旧值更大. 但是,如果将添加到2D矩阵中,即使在这种情况下,也需要复制内容,因为Matlab将行以矩阵形式存储在内存中. /p>

Continuity of memory chunks: sometimes Matlab is able to allocate new memory continuously at the end of the old matrix. In principle, in such a scenario the old values need not be copied to the new location - since it is the same as the old one just bigger. However, if you add rows to a 2D matrix, the content needs to be copied even in this scenario, since Matlab stores matrices in a row-major fashion in memory.

所以,我的答案是这样的:

So, my answer is this:

首先,您确切不知道矩阵的大小:如果知道一个维,则使它等于矩阵的行数,因此只需要更改列数即可.这样,如果您需要复制已经存储的数据,则将以更大的块进行复制.

First of all, what exactly don't you know about the size of the matrix: if you know one dimension - make it the number of rows of your matrix, so you'll only need to change the number of columns. This way, if your already stored data needs to be copied, it would be copied at larger chunks.

第二,这取决于您有多少可用RAM. 如果您不缺RAM,那么高估就没有错.

Second, it depends on how much free RAM you have at your disposal. If you are not short at RAM, then there's nothing wrong with over estimating.

但是,如果您的RAM不足,请考虑不足的估计. 但是,当您重新分配时,请在每次迭代时增加新的块大小:

However, if you are short at RAM, consider under estimating. BUT when you re-allocate, increase the new block size at each iteration:

BASIC_SIZE = X;  % first estimate
NEW_SIZE = Y;    % if need more, add this amount
factor = 2;      
arr = zeros( m, BASIC_SIZE ); % first allocation, assuming we know number of rows
while someCondition
    % process arr ...
    if needMoreCols
        arr(:, size(arr,2) + (1:NEW_SIZE) ) = 0; % allocate another block
        NEW_SIZE = round(NEW_SIZE * factor);  % it seems like we are off in estimation, try larger chunk next time factor should be > 1
    end
end
arr = arr(:, 1:actualNumOfCols ); % resize to actual size, discard unnecessary columns

这篇关于Matlab预分配,猜是一个大矩阵还是一个小矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆