From help-octave-request at bevo dot che dot wisc dot edu Thu Dec 11 20:32:55 2003 Subject: Re: Arguments out? From: Paul Kienzle To: Vic Norton Cc: help-octave at bevo dot che dot wisc dot edu, Thorsten Meyer Date: Thu, 11 Dec 2003 21:31:56 -0500 On Dec 11, 2003, at 6:05 PM, Vic Norton wrote: > Yeah, I thought about that later, Thorsten. Thanks for pointing it out. > > So when you write something like > > function [U, S, V] = svd(A) > > and A comes in as an m x n, you might do the computations to get > > U - orthogonal m x m > S - diagonal m x n > V - orthogonal n x n > > satisfying > > A = U * S * V' > > Then you look at nargout. > > You will only accept nargout = 0, 1, or 3. If nargout = 0 or 1, you > want to return diag(S). If nargout = 3, you want to return the triple > [U, S, V]. Apparently your final lines should read something like this > > if (nargout < 2) > U = diag(S); > elseif (nargout != 3) > error("you don't know what you are doing, dummy!"); > endif > > This is what bothers me. U and diag(S) are completely different kinds > of objects. Writing U = diag(S) simply goes against my grain. So write it using varargout: function varargout = svd(A) ... switch nargout {0,1}, varargout = { diag(s) }; 3, varargout = { u, s, v }; otherwise, usage("sigma=svd(A) or [u,s,v]=svd(A)"); end end or equivalently using meaningless placeholders for the returned values: function [r1,r2,r3] = mysvd(A) ... if nargout<=1, r1=diag(s); else r1=u; r2=s; r3=v; end end Paul Kienzle pkienzle at users dot sf dot net ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------