How to use sum(A, vecdim) In MATLAB

This function is used to sum the elements of A based on the dimensions specified in the vector vecdim.

Example:

Matlab




% MATLAB code for sum(A,'vecdim')
% Initializing an array A
A = [1 3 5; 2 4 6; 7 9 11; 8 10 12; 13 14 15]
  
% Calling the sum() function
% over the above array along 
% vecdim of [2, 3]
Sum = sum(A, [2, 3])


Output:

A =
   1    3    5
   2    4    6
   7    9   11
   8   10   12
  13   14   15
Sum =
   9
  12
  27
  30
  42

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.

Similar Reads

Using sum(A)

This is used to return the sum of the elements of the array along the first array dimension whose size does not equal 1. It returns a row vector containing the sum of each column....

Using sum(A, ‘all’)

...

Using sum(A, dim)

sum(A, ‘all’) is used to calculate the sum of all elements of A. And this syntax is valid only for MATLAB versions R2018b and later....

Using sum(A, vecdim)

...

sum(___, outtype)

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....

Using sum(___, nanflag)

...