MATLAB 中的矩阵数组
我正在寻找一种方法在 MATLAB 的数组中存储大量可变数量的矩阵。
有什么方法可以实现这一目标吗?
示例:
for i: 1:unknown
myArray(i) = zeros(500,800);
end
如果数组的变化长度未知,我可以根据需要使用附加信息进行修改。
更新: 性能是我努力实现这一目标的主要原因。 我之前有过它,它将数据作为单个矩阵抓取,实时显示,然后继续处理下一组数据。
我按照 Rocco 下面的建议尝试使用多维数组,但是我的数据太大以至于内存不足,我可能不得不为我的情况寻找另一种替代方案。 当我尝试其他建议时会更新。
更新2: 谢谢大家的建议,但是我应该事先指定,精度和速度都是这里不可或缺的因素,在尝试 3-d 数组之前我可能必须考虑回到原来的方法并重新评估导入数据。
I am looking for a way to store a large variable number of matrixes in an array in MATLAB.
Are there any ways to achieve this?
Example:
for i: 1:unknown
myArray(i) = zeros(500,800);
end
Where unknown is the varied length of the array, I can revise with additional info if needed.
Update:
Performance is the main reason I am trying to accomplish this. I had it before where it would grab the data as a single matrix, show it in real time and then proceed to process the next set of data.
I attempted it using multidimensional arrays as suggested below by Rocco, however my data is so large that I ran out of Memory, I might have to look into another alternative for my case. Will update as I attempt other suggestions.
Update 2:
Thank you all for suggestions, however I should have specified beforehand, precision AND speed are both an integral factor here, I may have to look into going back to my original method before trying 3-d arrays and re-evaluate the method for importing the data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用元胞数组。 与 3D 数组相比,它的优势在于它不需要连续的内存空间来存储所有矩阵。 事实上,每个矩阵都可以存储在内存中的不同空间中,如果您的可用内存碎片,这将使您避免内存不足错误。 下面是一个在元胞数组中创建矩阵的示例函数:
使用它:
并访问元素:
如果您无法提前知道矩阵的数量,您可以简单地使用 MATLAB 的动态索引来使数组尽可能大根据您的需要。 性能开销与单元格数组的大小成正比,并且不受矩阵本身大小的影响。 例如:
Use cell arrays. This has an advantage over 3D arrays in that it does not require a contiguous memory space to store all the matrices. In fact, each matrix can be stored in a different space in memory, which will save you from Out-of-Memory errors if your free memory is fragmented. Here is a sample function to create your matrices in a cell array:
To use it:
And to access your elements:
If you can't know the number of matrices in advance, you could simply use MATLAB's dynamic indexing to make the array as large as you need. The performance overhead will be proportional to the size of the cell array, and is not affected by the size of the matrices themselves. For example:
如果所有矩阵的大小相同(即 500x800),那么您可以创建一个 3D 数组:
要访问一个数组,您可以使用以下语法:
您可以通过多种方式向 myArray 添加更多矩阵:
如果每个矩阵的大小不相同,则需要使用 Hosam 建议的元胞数组。
编辑:我错过了关于内存不足的部分。 我猜你的 nUnknown 相当大。 您可能必须切换矩阵的数据类型(如果使用整数,则为单个或什至 uintXX 类型)。 您可以在对零的调用中执行此操作:
If all of the matrices are going to be the same size (i.e. 500x800), then you can just make a 3D array:
To access one array, you would use the following syntax:
You can add more matrices to myArray in a couple of ways:
If each matrix is not going to be the same size, you would need to use cell arrays like Hosam suggested.
EDIT: I missed the part about running out of memory. I'm guessing your nUnknown is fairly large. You may have to switch the data type of the matrices (single or even a uintXX type if you are using integers). You can do this in the call to zeros:
如果内存不足,请在系统中添加更多 RAM,并确保您运行的是 64 位操作系统。 还可以尝试降低精度(您真的需要双精度还是可以使用单精度?):
要附加到该数组,请尝试:
If you're running out of memory throw more RAM in your system, and make sure you're running a 64 bit OS. Also try reducing your precision (do you really need doubles or can you get by with singles?):
To append to that array try:
我正在八度(matlab 克隆)中进行一些体积渲染,并使用内存消耗构建我的 3D 数组(即 2d 切片数组)
似乎很有效。 (对于后续的计算速度不能说同样的事情:^)
I was doing some volume rendering in octave (matlab clone) and building my 3D arrays (ie an array of 2d slices) using
Memory consumption seemed to be efficient. (can't say the same for the subsequent speed of computations :^)
如果你知道什么是未知,
你可以做类似的事情
,但是距离我上次使用 matlab 已经有一段时间了。
所以这个页面可能会阐明这个问题:
http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_prog/f1-86528.html
if you know what unknown is,
you can do something like
However it has been a while since I last used matlab.
so this page might shed some light on the matter :
http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_prog/f1-86528.html
就这样做
just do it like this