📄 gcd1.m
字号:
function [g,h] = gcd1(y,x)
% GCD1 Greatest common divisor of integers or rational fractions.
%
% G = GCD1(X) returns the GCD of elements of X which must be integers.
%
% [G,H] = GCD1(X,Y) returns gcd=G/H of rational fractions X(i)/Y(i)
% whose numerators are in X and denominators in Y.
% Note: X and Y must be the SAME SIZE.
% Note: G and H are scalars.
%
% % To find the GCD of 8 12 32 and 68
% >>hl = gcd1([8 12 32 68])
%
% GCD1 (with no input arguments) invokes the following example
%
% % Find the GCD of (5/2), (4/3), and (6/14),
% >>[num,den] = gcd1([5 4 6],[2 3 14])
%
% ADSP Toolbox: Version 2.0
% For use with "Analog and Digital Signal Processing", 2nd Ed.
% Published by PWS Publishing Co.
%
% Ashok Ambardar, EE Dept. MTU, Houghton, MI 49931, USA
% http://www.ee.mtu/faculty/akambard.html
% e-mail: akambard@mtu.edu
% Copyright (c) 1998
if nargin==0,help gcd1,disp('Strike a key to see results of the example')
pause,[num,den]=gcd1([5 4 6],[2 3 14]),return,end
%Uses Euclid's algorithm. Ref: Maple V Tutorial.
y=y(:);yr=y-round(y);if any(any(yr)),error('gcd1 uses integers'),return,end
y=abs(y);h=1;l=length(y);if l==1,g=y;if nargin>1,h=x;end,return,end
if nargin>1
x=x(:);xr=x-round(x);if any(any(xr)),error('gcd1 uses integers'),return,end
x=abs(x);for i=1:l,g1=x(i);b=y(i);while b,d=rem(g1,b);g1=b;b=d;end
if g1~=1,x(i)=x(i)/g1;y(i)=y(i)/g1;end,end,end
g=y(1);for j=2:l,b=y(j);while b,d=rem(g,b);g=b;b=d;end,end
if nargin>1,h=lcm1(x);end
%if nargin>1,h=lcm1(x);
%g1=g;b=h;while b,d=rem(g1,b);g1=b;b=d;end,g=g/g1;h=h/g1;end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -