元胞数组中矩阵的元素总和

Elementwise sum of matrices in cell array

我有一个元胞数组如下:

A = { [1 2;3 4] ;[5 6;7 8]};

如何添加

A{1,1} + A{2,1} = {[6 8;10 12]};

Matlab 中没有循环?

假设元胞数组的每个条目都具有相同的维度,之后您需要 concatenate them into the third dimension and sum 它们:

%// concatenate along third dimension
B = cat(3, A{:})
%// sum along third dimension
C = sum(B,3)

如果元胞数组的大小为 2,您可以简单地执行以下操作:

result = plus(A{:});

这会在从二元元胞数组生成的 comma-separated list 上调用 plus(加法)。