Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.fr>
Newsgroups: comp.soft-sys.matlab
Subject: Re: linear equation with restrictions
Date: Sun, 20 Jul 2008 12:19:01 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 49
Message-ID: <g5vafl$j4j$1@fred.mathworks.com>
References: <g5sgla$b7r$1@fred.mathworks.com> <g5sjfa$fbg$1@fred.mathworks.com> <g5td8a$s9q$1@fred.mathworks.com> <g5tep3$g7r$1@fred.mathworks.com> <g5thqi$h7d$1@fred.mathworks.com> <g5tj02$rj4$1@fred.mathworks.com> <g5tleq$siu$1@fred.mathworks.com> <g5ujb2$7c1$1@fred.mathworks.com> <g5v5rm$iav$1@fred.mathworks.com>
Reply-To: "Bruno Luong" <b.luong@fogale.fr>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1216556341 19603 172.30.248.38 (20 Jul 2008 12:19:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 20 Jul 2008 12:19:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:480538


For you Leon,

Bruno


function [g varargout] = gcdn(varargin)
% function g = gcdn(a1, a2, ..., an); OR
%
% return g, Greatest common divisor of a1, ... an
% 
% [g c1 c2 ... cn]=gcdn(a1, a2, ..., an)
%   return also c1, ..., cn
% So that a1*c1 + ... an*cn = g
% 
% Compact calling
%   [g c]=gcdn(a1, a2, ..., an) or [g c]=gcdn(a)
%   assumes a and c are array
%

if nargin<2
    a = varargin{1};
    if length(a)<2
        error('gcdn requires two or more inputs');
    end
    a = reshape(a,1,[]);
else
    % Put all numbers in array
    a=cell2mat(varargin);
end

g=a(1);
c=zeros(size(a));
c(1)=1;
for k=2:length(a)
    [g cg c(k)]= gcd(g, a(k));
    c(1:k-1) = c(1:k-1)*cg;
end

if nargout>=2
    switch (nargout-1)
        case 1,
            varargout{1}=c;
        case length(c)
            varargout=num2cell(c);
        otherwise
             error('number of output is incompatible with
input');
    end
end