Return specific value in MATLAB -
i need write function in matlab that, given birth dates of 2 people in form ( y1,m1,d1,y2,m2,d2 )
(year, month, day) compare them , return 1 if first person older, -1 if first person younger , 0 if have same age. have done following:
function classify( y1,m1,d1,y2,m2,d2 ) if y1 > y2 return -1; elseif y1 < y2 return 1; elseif y1 == y2 if m1 > m2 return -1; elseif m1 < m2 return 1; elseif m1 == m2 if d1 > d2 return -1; elseif d1 < d2 return 1; elseif d1 == d2 return 0; end end end end
but gives error. how return value in matlab without having declare variable? need return 1,0,-1 depending on result, , seems return 1;
not work well.
to have function return value, need declare corresponding variables in function definition line. return
function exits function prematurely, not return values itself.
function output = functionname() %# assign output output = 1; end
similarly how can input multiple values, can have return multiple values
function [add, mult] = addandmultiply(a,b) add = a+b; mult = a*b; end
call as
[u,v] = addandmultiply(1,2);
now, specific problem compare 2 dates: recommend using datenum
:
function firstisolder = classify( y1,m1,d1,y2,m2,d2 ) firstdate = datenum(y1, m1, d1); seconddate = datenum(y2, m2, d2); %# if first older, sign +1, if both equal, sign 0 firstisolder = sign(seconddate - firstdate);
Comments
Post a Comment