Is there a concise way of summing over multiple dimensions of an array?
22 次查看(过去 30 天)
显示 更早的评论
Hello all,
Is there a concise way of summing over multiple dimensions, i.e. using just one sum function? I know if I have a defined array A, then I can do:
sum(A(:));
But what if I want to sum over a range of that array or some kind of calculated array? I would either have to use sum of sum of sum and so on to get the total sum or assign a variable to that temporary array? Thank you!
采纳的回答
更多回答(2 个)
Pooja Lalan
2018-11-7
编辑:Pooja Lalan
2018-11-7
Starting in R2018b, you can use this syntax
sum( SomeArray(1:18, 5:43, :, 2:9), 'all' )
sum( SomeArray(1:18, 5:43, :, 2:9), [1 2 3 4] )
0 个评论
Guillaume
2017-12-4
Not sure it is concise, it avoids the multiple sum:
sum(reshape(A(yourange), 1, []))
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!