How to use sum(A, dim) In MATLAB
sum(A, dim) is used to return the sum along dimension dim. For example, sum(A, 2) is a column vector containing the sum of each row.
Example:
Matlab
% demonstration of MATLAB sum(A,'dim')function % Initializing an array A A = [1 3 5; 2 4 6; 7 9 11; 8 10 12] % Calling the sum() function % over the above array along dim 2 Sum = sum(A, 2) |
Output:
A = 1 3 5 2 4 6 7 9 11 8 10 12 Sum = 9 12 27 30
How to find sum of elements of an array in MATLAB?
This article will discuss the “Finding sum of elements of an array” in MATLAB that can be done using multiple approaches which are illustrated below.