Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Greatest commmon divisor
Date: Tue, 16 Dec 2008 07:59:07 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 33
Message-ID: <gi7n4b$c07$1@fred.mathworks.com>
References: <ghukju$ksu$1@fred.mathworks.com> <ghul2u$rfp$1@fred.mathworks.com> <ghum4l$d95$1@fred.mathworks.com> <gi7mqt$87b$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1229414347 12295 172.30.248.37 (16 Dec 2008 07:59:07 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 16 Dec 2008 07:59:07 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 870065
Xref: news.mathworks.com comp.soft-sys.matlab:507242


"Jos " <#10584@fileexchange.com> wrote in message <gi7mqt$87b$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <ghum4l$d95$1@fred.mathworks.com>...
> > "Feng" <nclxin@hotmail.com> wrote in message <ghul2u$rfp$1@fred.mathworks.com>...
> > > "Feng" <nclxin@hotmail.com> wrote in message <ghukju$ksu$1@fred.mathworks.com>...
> > > > Hi, any one know how can get the gcd for an array. The function gcd() in matlabe > will give result for two scalar numbers.
> > > > for example I want to get 2 for the array A= [10 2 10 -4 2 0];
> > > > Is there any function to do that? or how can I do that? 
> > > > 
> > > > Thanks
> > 
> >   You could always use an old-fashioned for-loop:
> > 
> > c = A(1);
> > for k = 2:length(A)
> >  c = gcd(c,A(k));
> > end
> > 
> > Roger Stafford
> 
> 
> What about
> 
> min(gcd(A(1:end-1),A(2:end)))
> 
> Jos

On second thoughts, you have to remove the zeros first, otherwise something like
A = [x 0 0 y] 
will always give 0, instead of gcd(x,y)

Jos