📄 lcm1.m
字号:
function [h,g] = lcm1(y,x)
% LCM1 Least Common Multiple
%
% H = LCM1(X) returns the LCM of elements of X which must be integers.
% [H,G] = LCM1(X,Y) returns the LCM H/G of rational fractions X(i)/Y(i)
% where X and Y correspond to the numerator and denominator values.
% Note: X and Y must be the SAME SIZE.
% Note: H and G are scalars.
%
% % To find the LCM of 8 12 32 and 68
% >>hl = lcm1([8 12 32 68])
%
% LCM1 (with no input arguments) invokes the following example:
%
% % For the LCM of (2/5), (3/4), and (14/6)
% >>[num,den] = lcm1([2 3 14],[5 4 6])
% Modified version of LCM to accomodate matrices or rational fractions.
% Modified by A. Ambardar 05-11-93. With permission from The Mathworks, Inc.
% 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 lcm1,disp('Strike a key to see results of last example')
pause,[num,den]=lcm1([2 3 14],[5 4 6]),return,end
y=y(:);yr=y-round(y);if any(any(yr)),error('lcm1 uses integers'),return,end
y=abs(y);g=1;l=length(y);if l==1,h=y;if nargin>1,g=x;end,return,end
if nargin>1
x=x(:);xr=x-round(x);if any(any(xr)),error('lcm1 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
h=y(1);for j=2:l,a=h;b=y(j);while b,d=rem(a,b);a=b;b=d;end,h=h*y(j)/a;end
if nargin>1,g=gcd1(x);c=gcd1([g h]);h=h/c;g=g/c;end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -