Mean of rows and columns of several matrices in a cell
3次观看(最近30天)
Show older comments
I have say 'n' axb matrices and i want to generate a new matrix of dimension axb which is the mean of all 'n' axb matrices, i.e the first element of this new matrix is the mean of all first elements in each 'n' axb matrices and so on. Is there a way to compute this average matrix from a group of matrices in matlab? I had tried to do this by creating a cell but couldn't figure out how to take mean of each element of these matrices. I would appreciate any ideas or suggestions.
Thank you!
0 Comments
Accepted Answer
Stephen23
on 8 Sep 2015
编辑:Stephen23
on 8 Sep 2015
MATLAB is
fastest
和
easiest
when you stick with using numeric arrays and learn how to use the
dimensions
of the arrays, rather than sticking data in complicated data classes like cell arrays and structures. (most importantly this includes learning how to write
矢量化代码
). If you use the
第三
数组的维度,那么您的问题很容易使用
意思是
:
>> X(:,:,3) = [1,2;3,4];
>> X(:,:,2) = [5,6;7,8];
>> X(:,:,1) = [9,0;1,2];
>> mean(X,3)
ans =
5.0000 2.6667
3.6667 4.6667
The
最好的
solution is to create your
n
matrices in this single numeric array. The second best solution is to create your arrays inside a cell array, which can then be concatenated together like this:
>> y {3} = [1,2; 3,4];
>> Y{2} = [5,6;7,8];
>> Y{1} = [9,0;1,2];
>> X = cat(3,Y{:})
>> mean(X,3)
You will find that the
X
与第一个示例相同。
The one method you should
not
使用是动态创建
n
matrices as individual variables (e.g. sequentially numbered) and then attempt to join them together:
creating variable names dynamically is buggy, slow and a very poor programming practice
.