Efficient way to sum the components of a matrix
2 次查看(过去 30 天)
显示 更早的评论
I have an array of unknown dimensions. I want to sum the values of this array. I need to do this many times. My current code is:
% Determine the number of dimensions in the array.
m = ndims(x);
% Assign the value of the array to a temporary working array, 'temp'.
temp = x;
% Calculate the total of the working array, 'temp'.
for i = 1:m
temp = sum(temp);
end
I have to do this many many times, and profile viewer is showing the command 'sum' as taking a large portion of time. Is there a more efficient way of doing this, or am I just stuck with this method?
5 个评论
采纳的回答
Joseph Cheng
2014-6-24
if you are just looking for the total sum of the n dimensional array, then you can just do sum(temp(:));
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!