Thread Subject: Greatest commmon divisor

Subject: Greatest commmon divisor

From: Feng

Date: 12 Dec, 2008 21:21:02

Message: 1 of 11

Hi, any one know how can get the gcd for an array. The function gcd() in matlabe will give result for a array.
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

Subject: Greatest commmon divisor

From: Feng

Date: 12 Dec, 2008 21:29:02

Message: 2 of 11

"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

Subject: Greatest commmon divisor

From: Roger Stafford

Date: 12 Dec, 2008 21:47:02

Message: 3 of 11

"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

Subject: Greatest commmon divisor

From: Loren Shure

Date: 16 Dec, 2008 01:38:23

Message: 4 of 11

In article <ghukju$ksu$1@fred.mathworks.com>, nclxin@hotmail.com says...
> Hi, any one know how can get the gcd for an array. The function gcd() in matlabe will give result for a array.
> 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
>

Check out the function arrayfun

--
Loren
http://blogs.mathworks.com/loren

Subject: Greatest commmon divisor

From: Bruno Luong

Date: 16 Dec, 2008 05:51:02

Message: 5 of 11

Loren Shure <loren@mathworks.com> wrote in message <MPG.23b0d09e505fb8f4989907@news.mathworks.com>...
> >
>
> Check out the function arrayfun
>

I can't see how arrayfun would be used here. I rather stick with Roger's for-loop solution.

Bruno

Subject: Greatest commmon divisor

From: Jos

Date: 16 Dec, 2008 07:54:05

Message: 6 of 11

"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

Subject: Greatest commmon divisor

From: Jos

Date: 16 Dec, 2008 07:59:07

Message: 7 of 11

"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

Subject: Greatest commmon divisor

From: Bruno Luong

Date: 16 Dec, 2008 08:09:06

Message: 8 of 11


>
> min(gcd(A(1:end-1),A(2:end)))
>

Jos, this does not work

A=[15 30 20]

gcd is 5, not 10

Bruno

Subject: Greatest commmon divisor

From: Roger Stafford

Date: 16 Dec, 2008 08:16:05

Message: 9 of 11

"Jos " <#10584@fileexchange.com> wrote in message <gi7mqt$87b$1@fred.mathworks.com>...
> What about
>
> min(gcd(A(1:end-1),A(2:end)))
>
> Jos

  I don't think that would work, Jos, even with zeros eliminated. Let A = [15,21,35]. Then gcd(A(1:end-1),A(2:end)) would yield [3,7] but the minimum of these is not the gcd of the three combined quantities. The greatest common factor of all three is simply 1, not this minimum.

Roger Stafford

Subject: Greatest commmon divisor

From: Jos

Date: 16 Dec, 2008 10:04:03

Message: 10 of 11

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gi7o45$297$1@fred.mathworks.com>...
> "Jos " <#10584@fileexchange.com> wrote in message <gi7mqt$87b$1@fred.mathworks.com>...
> > What about
> >
> > min(gcd(A(1:end-1),A(2:end)))
> >
> > Jos
>
> I don't think that would work, Jos, even with zeros eliminated. Let A = [15,21,35]. Then gcd(A(1:end-1),A(2:end)) would yield [3,7] but the minimum of these is not the gcd of the three combined quantities. The greatest common factor of all three is simply 1, not this minimum.
>
> Roger Stafford

Yep, you and Bruno are right, of course. I was just wondering why Loren suggested arrayfun, and so I was looking for a function that could do this.

Jos

Subject: Greatest commmon divisor

From: Loren Shure

Date: 16 Dec, 2008 15:16:09

Message: 11 of 11

In article <gi7uei$22l$1@fred.mathworks.com>, #10584@fileexchange.com
says...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gi7o45$297$1@fred.mathworks.com>...
> > "Jos " <#10584@fileexchange.com> wrote in message <gi7mqt$87b$1@fred.mathworks.com>...
> > > What about
> > >
> > > min(gcd(A(1:end-1),A(2:end)))
> > >
> > > Jos
> >
> > I don't think that would work, Jos, even with zeros eliminated. Let A = [15,21,35]. Then gcd(A(1:end-1),A(2:end)) would yield [3,7] but the minimum of these is not the gcd of the three combined quantities. The greatest common factor of all three is simply 1, not this minimum.
> >
> > Roger Stafford
>
> Yep, you and Bruno are right, of course. I was just wondering why Loren suggested arrayfun, and so I was looking for a function that could do this.
>
> Jos
>
>

Perhaps I misread the problem. I thought the OP wanted GCD of each
"pair" of points in 2 arrays. Probably read too fast...

--
Loren
http://blogs.mathworks.com/loren

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com