MATLAB 中的矩阵数组

发布于 2024-07-12 10:00:14 字数 453 浏览 6 评论 0原文

我正在寻找一种方法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

呆° 2024-07-19 10:00:14

使用元胞数组。 与 3D 数组相比,它的优势在于它不需要连续的内存空间来存储所有矩阵。 事实上,每个矩阵都可以存储在内存中的不同空间中,如果您的可用内存碎片,这将使您避免内存不足错误。 下面是一个在元胞数组中创建矩阵的示例函数:

function result = createArrays(nArrays, arraySize)
    result = cell(1, nArrays);
    for i = 1 : nArrays
        result{i} = zeros(arraySize);
    end
end

使用它:

myArray = createArrays(requiredNumberOfArrays, [500 800]);

并访问元素:

myArray{1}(2,3) = 10;

如果您无法提前知道矩阵的数量,您可以简单地使用 MATLAB 的动态索引来使数组尽可能大根据您的需要。 性能开销与单元格数组的大小成正比,并且不受矩阵本身大小的影响。 例如:

myArray{1} = zeros(500, 800);
if twoRequired, myArray{2} = zeros(500, 800); end

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:

function result = createArrays(nArrays, arraySize)
    result = cell(1, nArrays);
    for i = 1 : nArrays
        result{i} = zeros(arraySize);
    end
end

To use it:

myArray = createArrays(requiredNumberOfArrays, [500 800]);

And to access your elements:

myArray{1}(2,3) = 10;

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:

myArray{1} = zeros(500, 800);
if twoRequired, myArray{2} = zeros(500, 800); end
著墨染雨君画夕 2024-07-19 10:00:14

如果所有矩阵的大小相同(即 500x800),那么您可以创建一个 3D 数组:

nUnknown;  % The number of unknown arrays
myArray = zeros(500,800,nUnknown);

要访问一个数组,您可以使用以下语法:

subMatrix = myArray(:,:,3);  % Gets the third matrix

您可以通过多种方式向 myArray 添加更多矩阵:

myArray = cat(3,myArray,zeros(500,800));
% OR
myArray(:,:,nUnknown+1) = zeros(500,800);

如果每个矩阵的大小不相同,则需要使用 Hosam 建议的元胞数组。

编辑:我错过了关于内存不足的部分。 我猜你的 nUnknown 相当大。 您可能必须切换矩阵的数据类型(如果使用整数,则为单个或什至 uintXX 类型)。 您可以在对零的调用中执行此操作:

myArray = zeros(500,800,nUnknown,'single');

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

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);

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:

myArray = zeros(500,800,nUnknown,'single');
心如狂蝶 2024-07-19 10:00:14
myArrayOfMatrices = zeros(unknown,500,800);

如果内存不足,请在系统中添加更多 RAM,并确保您运行的是 64 位操作系统。 还可以尝试降低精度(您真的需要双精度还是可以使用单精度?):

myArrayOfMatrices = zeros(unknown,500,800,'single');

要附加到该数组,请尝试:

myArrayOfMatrices(unknown+1,:,:) = zeros(500,800);
myArrayOfMatrices = zeros(unknown,500,800);

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?):

myArrayOfMatrices = zeros(unknown,500,800,'single');

To append to that array try:

myArrayOfMatrices(unknown+1,:,:) = zeros(500,800);
如梦亦如幻 2024-07-19 10:00:14

我正在八度(matlab 克隆)中进行一些体积渲染,并使用内存消耗构建我的 3D 数组(即 2d 切片数组)

buffer=zeros(1,512*512*512,"uint16");
vol=reshape(buffer,512,512,512);

似乎很有效。 (对于后续的计算速度不能说同样的事情:^)

I was doing some volume rendering in octave (matlab clone) and building my 3D arrays (ie an array of 2d slices) using

buffer=zeros(1,512*512*512,"uint16");
vol=reshape(buffer,512,512,512);

Memory consumption seemed to be efficient. (can't say the same for the subsequent speed of computations :^)

一抹苦笑 2024-07-19 10:00:14

如果你知道什么是未知,

你可以做类似的事情

myArray = zeros(2,2);
for i: 1:unknown
  myArray(:,i) = zeros(x,y);
end

,但是距离我上次使用 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

myArray = zeros(2,2);
for i: 1:unknown
  myArray(:,i) = zeros(x,y);
end

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

谈下烟灰 2024-07-19 10:00:14

就这样做

x=zeros(100,200);
for i=1:100
  for j=1:200
    x(i,j)=input('enter the number');
  end
end

just do it like this

x=zeros(100,200);
for i=1:100
  for j=1:200
    x(i,j)=input('enter the number');
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文