📜  如何在MATLAB中找到数组元素的总和?

📅  最后修改于: 2022-05-13 01:54:40.654000             🧑  作者: Mango

如何在MATLAB中找到数组元素的总和?

本文将讨论在 MATLAB 中可以使用多种方法完成的“查找数组元素的总和”,如下所示。

使用 sum(A)

这用于返回沿大小不等于 1 的第一个数组维度的数组元素的总和。它返回一个包含每列总和的行向量。

例子:

Matlab
% MATLAB code for sum(A)
% Initializing an array A
A = [1 2 3; 4 5 6]
  
% Calling the sum() function
% over the above array
Sum = sum(A)


Matlab
% MATLAB code for sum(A,'all') 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
Sum = sum(A)


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)


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])


Matlab
% MATLAB code for sum(A,'outtype')
% Creating a vector of 32-Obit integers
A = int32(1:5);
  
% Calculating the above vector's
% elements by specifying the output type as native
Sum = sum(A,'native')


Matlab
% MATLAB code for sum() with  NaN values
% Creating a vector of some numbers and NaN Values 
A = [1 -0.05 10.45 NaN 0.8 NaN 1.8 NaN];
  
% Calculating sum of the above vector
% excluding NaN values
Sum = sum(A, 'omitnan')


Matlab
% MATLAB code for addition of 
% Using sum() function over sum() function.
% Initializing an array A
A = [1 2 3; 4 5 6]
  
% Calling the sum() function
% over the above array
Sum = sum(sum(A))


Matlab
% MATLAB code for addition of 
% using sum() function over sum() 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
Sum = sum(sum(A))


输出:



A =
  1   2   3
  4   5   6
Sum =
  5   7   9

使用 sum(A, 'all')

sum(A, 'all') 用于计算 A 的所有元素的总和。 此语法仅对 MATLAB 版本 R2018b 及更高版本有效。

例子:

MATLAB

% MATLAB code for sum(A,'all') 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
Sum = sum(A)

输出:

A =
   1    3    5
   2    4    6
   7    9   11
   8   10   12
Sum =
  18   26   34

使用 sum(A, dim)

sum(A, dim)用于返回沿维度 dim 的总和。例如,sum(A, 2) 是一个列向量,其中包含每行的总和。

例子:

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)

输出:



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

使用 sum(A, vecdim)

此函数用于根据向量 vecdim 中指定的维度对 A 的元素求和。

例子:

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])

输出:

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

总和(___,外型)

sum(___, outtype)用于返回具有指定数据类型的总和,使用前面语法中的任何输入参数。这里的输出类型可以是“default”、“double”或“native”。

例子:

MATLAB

% MATLAB code for sum(A,'outtype')
% Creating a vector of 32-Obit integers
A = int32(1:5);
  
% Calculating the above vector's
% elements by specifying the output type as native
Sum = sum(A,'native')

输出:

Sum = 15

使用 sum(___, nanflag)

sum(___, nanflag)用于指定是在计算中包含还是省略 NaN 值,用于任何先前的语法。 sum(A,'includenan') 包括计算中的所有 NaN 值,而 sum(A,'omitnan') 忽略它们。

例子:



MATLAB

% MATLAB code for sum() with  NaN values
% Creating a vector of some numbers and NaN Values 
A = [1 -0.05 10.45 NaN 0.8 NaN 1.8 NaN];
  
% Calculating sum of the above vector
% excluding NaN values
Sum = sum(A, 'omitnan')

输出:

Sum =  14

让我们看看在 sum()函数上添加 Using sum()函数。它将完全返回数组元素的总和。

示例 1:

MATLAB

% MATLAB code for addition of 
% Using sum() function over sum() function.
% Initializing an array A
A = [1 2 3; 4 5 6]
  
% Calling the sum() function
% over the above array
Sum = sum(sum(A))

输出:

A =
  1   2   3
  4   5   6
Sum =  21

示例 2:

MATLAB

% MATLAB code for addition of 
% using sum() function over sum() 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
Sum = sum(sum(A))

输出:

A =
   1    3    5
   2    4    6
   7    9   11
   8   10   12
Sum =  78