MATLAB 中的矩阵数组 [英] Array of Matrices in MATLAB
问题描述
我正在寻找一种方法来在 MATLAB 的数组中存储大量可变数量的矩阵.
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?
示例:
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.
我尝试使用 Rocco 在下面建议的多维数组,但是我的数据太大以至于内存不足,我可能不得不为我的案例寻找另一种选择.将在我尝试其他建议时更新.
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.
更新 2:谢谢大家的建议,但是我应该事先指定,精度和速度在这里都是一个不可或缺的因素,在尝试 3-d 数组之前,我可能不得不考虑回到我原来的方法,并重新评估导入的方法数据.
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.
推荐答案
如果所有矩阵的大小都相同(即 500x800),那么您可以制作一个 3D 数组:
If all of the matrices are going to be the same size (i.e. 500x800), then you can just make a 3D array:
nUnknown; % The number of unknown arrays
myArray = zeros(500,800,nUnknown);
要访问一个数组,您将使用以下语法:
To access one array, you would use the following syntax:
subMatrix = myArray(:,:,3); % Gets the third matrix
您可以通过以下几种方式向 myArray 添加更多矩阵:
You can add more matrices to myArray in a couple of ways:
myArray = cat(3,myArray,zeros(500,800));
% OR
myArray(:,:,nUnknown+1) = zeros(500,800);
如果每个矩阵的大小不同,您将需要使用 Hosam 建议的元胞数组.
If each matrix is not going to be the same size, you would need to use cell arrays like Hosam suggested.
我错过了关于内存不足的部分.我猜你的 nUnknown 相当大.您可能需要切换矩阵的数据类型(如果您使用的是整数,则为单个或什至 uintXX 类型).您可以在调用零时执行此操作:
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:
myArray = zeros(500,800,nUnknown,'single');
这篇关于MATLAB 中的矩阵数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!