简体   繁体   English

如何在单元格数组中添加矩阵?

[英]How can I add matrices inside a cell array?

I have a (1xn) cell array A where each matrix is a 1000x1000. 我有一个(1xn)单元格数组A,其中每个矩阵都是1000x1000。

Basically, I want to do A{1}+A{2}+A{3}+...+A{n} 基本上,我想做A {1} + A {2} + A {3} + ... + A {n}

I don't know the size of the array beforehand. 我事先不知道数组的大小。 So I should do a size(A) and add all the available matrices inside. 所以我应该做一个size(A)并在其中添加所有可用的矩阵。

  • I want to sum all of them using a loop 我想使用循环总结所有这些
  • But preferably in a vectorized way as I'll eventually add thousands of them and need the speed improvement. 但是最好以向量化的方式,因为我最终将添加数千个它们,并且需要提高速度。

How I store data into the cell array: 我如何将数据存储到单元格数组中:

for k = 1:length(pcpFileNames)
    pcp{k} = imread(pcpFileNames{k}); %Read all pcp files
end

where 

pcpFileNames = 

    'PCPRATE.20110612.000000.tif'
    'PCPRATE.20110612.000500.tif'
    'PCPRATE.20110612.001000.tif'
    'PCPRATE.20110612.001500.tif'
    'PCPRATE.20110612.002000.tif'
    'PCPRATE.20110612.002500.tif'
    'PCPRATE.20110612.003000.tif'
    'PCPRATE.20110612.003500.tif'
    'PCPRATE.20110612.004000.tif'
    'PCPRATE.20110612.004500.tif'

Proposed solution not working: 建议的解决方案不起作用:

pcpAccum = pcp{1};
for m = 2:numel(pcp)
    pcpAccum = pcpAccum + pcp{k};
end

This is giving me incorrect results. 这给了我错误的结果。 I checked one of the cells and it has zero value even though it should have 1.8 (example). 我检查了一个单元格,即使它应该有1.8,它也具有零值(示例)。


Working solution 工作方案

%Create a multidimensional array to store the data

precip = zeros(3501,7001,length(pcpFileNames)); %Pre-allocating an array

for k = 1:length(precipFileNames)
    precip(:,:,k) = imread(precipFileNames{k}); %Read all files
end

pcpAccum = sum(pcp,3); 

To add them in a vectorized way: 以向量化方式添加它们:

B = sum(cat(3,A{:}),3);

However, adding a lot of matrices like above way is a very bad idea if your matrices are already big. 但是,如果您的矩阵已经很大,则以上述方式添加许多矩阵是一个非常糟糕的主意。 Your 2D matrices are already in memory; 您的2D矩阵已经在内存中; to create the 3D matrix that will vectorize your addition will consume once again that amount of memory, but now in a contiguous memory area . 创建将矢量加法的3D矩阵将再次消耗该内存量,但是现在在连续的内存区域中 Due to memory fragmentation is quite likely that you'll have the total amount of memory to do the vectorized sum, but not in a contiguous area of memory ; 由于内存碎片,很有可能您将拥有全部的内存来执行矢量化总和,但不会在内存连续区域内 so your program will fail needlessly. 因此您的程序会不必要地失败。

What I would recommend in this case is a nice well-behaved loop: 在这种情况下,我建议的是一个行为良好的循环:

B = A{1};
for k = 2:numel(A)
        B = B + A{k};
end;

However, the loop has its own issues, and the speed is not one. 但是,循环有其自身的问题,并且速度不是一个。 The thing is that accumulating in an ever growing manner might give you precision errors (for floating point types) or integer overflows (for integer types). 事实是,以不断增长的方式进行累加可能会给您带来精度错误(对于浮点类型)或整数溢出(对于整数类型)。

To overcome the overflow errors caused by an integer type returned by imread , convert the data to a wider type ( double is the best bet): 为了克服由imread返回的整数类型引起的溢出错误,请将数据转换为更大的类型(最好的选择是double ):

B = double(A{1});
for k = 2:numel(A)
        B = B + double(A{k});
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM