how to use nargin function to take variable input arguments and return sum of those arguments

조회 수: 7 (최근 30일)
I am not sure how to use the nargin function. I wanted to take a variable number of input arguments and return a sum of those arguments. I am stuck. I have this code so far
function [ output_args ] = addmany() %This function will take zero or more numbers as a variable number of input %arguments and returns the sum of its input arguments. n=nargin; %number of input arguments sum
Not sure how to get a sum of the variable input arguments
  댓글 수: 4
Jan
Jan 2013년 10월 26일
Start with 2 inputs at first to get some experiences with the addition.

댓글을 달려면 로그인하십시오.

채택된 답변

Cedric
Cedric 2013년 10월 26일
편집: Cedric 2013년 10월 26일
Well, if you had a function
function x = doSomething(a, b, c, d)
...
end
nargin would tell you how many input arguments were present at the call, which would allow you e.g. to define default values for c and d if nargin were equal to 2. Now as you can see, this function has only 4 named input parameters, so this approach is not that well suited for a function which should accept any number of arguments.. what if a user calls your function with 500 arguments, are you going to define 500 named parameters just to be sure?
What we usually do is to use varargin, as follows
function x = doSomething(varargin)
...
end
In that case, varargin is a cell array inside the function, which contains all the arguments from the call.
Now as it's a homework, I cannot write the full code for you, but this hints sets you on a better track already. The next step should be to understand how to deal with cell arrays so you can compute a sum.
  댓글 수: 1
Cedric
Cedric 2013년 10월 27일
편집: Cedric 2013년 10월 27일
As Matt is pointing out, concatenating varargin developed as a Comma Separated List is probably the way we would get the content of varargin. If this looks too complicated to you, you could use CELL2MAT.. execute the following in the command window to understand how:
doc cell2mat

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2013년 10월 26일
편집: Matt J 2013년 10월 26일
it is a statement of homework but there is no specifics as to what commands are able to be used
If there are no restrictions, the better solution would be not to use separate arguments at all. Just put all arguments in a vector x and do sum(x).
function y = addmany(x)
y=sum(x);
end
>> y=addmany([1 2 3 4])
y =
10
  댓글 수: 2
KayLynn
KayLynn 2013년 10월 26일
I am trying to add a variable number of input arguments....Doesnt addmany(x) only add one of the inputs. Lets say I was trying to input ten varaibles ( just picking a random number), How would I be able to do so...Do i have to list each of the variable inputs or is there something I can do to take any variable number of inputs and find the sum of them
Matt J
Matt J 2013년 10월 27일
편집: Matt J 2013년 10월 27일
Doesnt addmany(x) only add one of the inputs.
Yes, but it's not clear why the summands have to be passed as separate input arguments rather than as elements of one single vector. Was that a requirement of the homework assignment?
or is there something I can do to take any variable number of inputs and find the sum of them
Yes, there is a way, and Cedric gave you strong hints. Here is another small one

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by