function [num,den] = lft2nd(m)
%   Converts an upper loop LFT on a repeated-scalar,
%   with scalar result, into a numerator/denominator
%   polynomial.

% Copyright 2003-2004 The MathWorks, Inc.

    szm = size(m);
    if szm(1)==szm(2)
        n = szm(1)-1;
        m11 = m(1:n,1:n);
        m12 = m(1:n,n+1);
        m21 = m(n+1,1:n);
        m22 = m(n+1,n+1);
        den = fliplr(poly(m11));
        fmat = zeros(n,n);
        fmat(1,:) = den(n+1)*m21;
        for k=1:n-1
            fmat(k+1,:) = fmat(k,:)*m11 + den(n-k+1)*m21;
        end
        num = m22*den + [fliplr((fmat*m12).') 0];
    else
        error('invalid m');
        return
    end
