Sum of many inputs
Show older comments
Hi guys,
I want to implement a function that I find the sum of as many input arguments I give.
For example in one case it can be (a+b). In another (a+b+c+d...) or (a+b+c).
I try to use varargin but don't know how.. Any suggestions?
thanx d.
Accepted Answer
More Answers (1)
Walter Roberson
on 26 Feb 2011
function S = sumthemall(varargin)
% Takes as many scalars, equal-length vectors, or
% equal-size arrays as you want, returns the sum.
if ~nargin
S = 0;
else
d = 1 + ndims(varargin{1});
S = sum( cat(d, varargin{:}), d );
end
end
Categories
Find more on MATLAB Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!