matlab中sum函数

2022-07-24 教育 93阅读
下面是matlab中sum函数的代码,可以看出里面也是用for循环实现的~~

function s = sum(A,dim)
%SUM Sum of the elements.
% For vectors, SUM(X) is the sum of the elements of X.
% For matrices, SUM(X) or SUM(X,1) is a row vector of column sums
% and SUM(X,2) is a column vector of row sums.
%
% See also SYM/PROD.

% Copyright 1993-2008 The MathWorks, Inc.

if isscalar(A) && strcmp(class(A.s),'maplesym')
if nargin == 1
s = sym(sum(A.s));
else
s = sym(sum(A.s,dim));
end
elseif nargin == 1 && any(size(A) == 1)
s = sym(0);
for k = 1:numel(A)
s = s + A(k);
end
elseif nargin == 1 || dim == 1
s = sym(zeros(1,size(A,2)));
for i = 1:size(A,1)
s = s + A(i,:);
end
else
s = sym(zeros(size(A,1),1));
for j = 1:size(A,2);
s = s + A(:,j);
end
end
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com