Summing all the Matrices in a Cell Array?
12 次查看(过去 30 天)
显示 更早的评论
Lets say i have a cell array with 3 7x2 matrices in it:
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/145979/image.jpeg)
.
How do i sum them up so to get a single 7x2 matrix?
In this exmaple, i only have 3 matrices.
But i may have a cell array with maybe 100+ matrices.
How should i do it?
1 个评论
Matt J
2014-11-5
编辑:Matt J
2014-11-5
You probably should not be in this situation to begin with. Whatever code you're using to create the cell array, should probably be organizing the data as a 7x2xP numeric array instead. Most operations on an MxNxP array will be more efficient than with the MxN slices split across P cells.
An exception to this is when M*N is large and you're trying to avoid the copies that MATLAB makes when you do things like
a=A(:,:,i)
but the slices would have to be very very large to warrant that.
采纳的回答
Azzi Abdelmalek
2014-11-5
编辑:Azzi Abdelmalek
2014-11-5
a={rand(7,2) rand(7,2) rand(7,2)} % This is an example
b=sum(cat(3,a{:}),3)
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!