Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
"Nice" matrix with orthonormal columns

Subject: "Nice" matrix with orthonormal columns

From: Andy

Date: 26 Jun, 2013 16:08:08

Message: 1 of 4

I assume the following is not built-in functionality, but MATLAB has enough built-in matrix functions that perhaps there is a slick way to do this which I'm just not seeing. I'd like to be able to create a matrix of a given size whose columns are orthonormal and whose entries are "not bad" (my hope is rationals). Any ideas?

Thanks in advance.

Subject: "Nice" matrix with orthonormal columns

From: Steven_Lord

Date: 26 Jun, 2013 17:22:17

Message: 2 of 4



"Andy " <myfakeemailaddress@gmail.com> wrote in message
news:kqf3l8$s60$1@newscl01ah.mathworks.com...
> I assume the following is not built-in functionality, but MATLAB has
> enough built-in matrix functions that perhaps there is a slick way to do
> this which I'm just not seeing. I'd like to be able to create a matrix of
> a given size whose columns are orthonormal and whose entries are "not bad"
> (my hope is rationals). Any ideas?
>
> Thanks in advance.

Depending on how much wiggle room you have on your "not bad" qualifier, the
'orthog' option for the GALLERY function may suit your needs. It doesn't
give you rationals but sums or products of square roots, trig functions, and
(in one case) exponentials.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: "Nice" matrix with orthonormal columns

From: Andy

Date: 27 Jun, 2013 00:39:06

Message: 3 of 4

"Steven_Lord" <slord@mathworks.com> wrote in message <kqf80a$ah1$1@newscl01ah.mathworks.com>...
>
>
> "Andy " <myfakeemailaddress@gmail.com> wrote in message
> news:kqf3l8$s60$1@newscl01ah.mathworks.com...
> > I assume the following is not built-in functionality, but MATLAB has
> > enough built-in matrix functions that perhaps there is a slick way to do
> > this which I'm just not seeing. I'd like to be able to create a matrix of
> > a given size whose columns are orthonormal and whose entries are "not bad"
> > (my hope is rationals). Any ideas?
> >
> > Thanks in advance.
>
> Depending on how much wiggle room you have on your "not bad" qualifier, the
> 'orthog' option for the GALLERY function may suit your needs. It doesn't
> give you rationals but sums or products of square roots, trig functions, and
> (in one case) exponentials.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com


That's probably not quite nice enough. I'm hoping to auto-generate some problems for a linear algebra exam which will be easily solvable by hand (no calculator).

Subject: "Nice" matrix with orthonormal columns

From: Andy

Date: 28 Jun, 2013 13:58:07

Message: 4 of 4

"Andy" wrote in message <kqg1ja$ert$1@newscl01ah.mathworks.com>...
> "Steven_Lord" <slord@mathworks.com> wrote in message <kqf80a$ah1$1@newscl01ah.mathworks.com>...
> >
> >
> > "Andy " <myfakeemailaddress@gmail.com> wrote in message
> > news:kqf3l8$s60$1@newscl01ah.mathworks.com...
> > > I assume the following is not built-in functionality, but MATLAB has
> > > enough built-in matrix functions that perhaps there is a slick way to do
> > > this which I'm just not seeing. I'd like to be able to create a matrix of
> > > a given size whose columns are orthonormal and whose entries are "not bad"
> > > (my hope is rationals). Any ideas?
> > >
> > > Thanks in advance.
> >
> > Depending on how much wiggle room you have on your "not bad" qualifier, the
> > 'orthog' option for the GALLERY function may suit your needs. It doesn't
> > give you rationals but sums or products of square roots, trig functions, and
> > (in one case) exponentials.
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > To contact Technical Support use the Contact Us link on
> > http://www.mathworks.com
>
>
> That's probably not quite nice enough. I'm hoping to auto-generate some problems for a linear algebra exam which will be easily solvable by hand (no calculator).

Here's a really horrible method. I've been away from MATLAB for a while, so this is not written well or efficiently, but it is completely straightforward. Since the goal is to auto-generate Gram-Schmidt problems for a linear algebra exam, this is good enough for small examples (e.g., size = [3,3]).

Output is a matrix A with integer entries and a matrix U which is the result of performing Gram-Schmidt on the columns of A, created in such a way that the columns of U don't involve any square roots or fraction denominators greater than 20. (So a student can be asked to use Gram-Schmidt on the columns of A without a calculator on a test, for example.)

function [A,U] = GS_example(size)

    % rrange is the range of random integers
    % small_squares is to make sure norms are perfect squares
    rrange = [-4,4];
    small_squares = (1:20).^2;
    tol = 1e-8;
    
    % initialize
    m = size(1);
    n = size(2);
    A = [];
        
    for ix = 1:n
        if ix > m
            break
        end
        next_col = sqrt(2); % value to make initial test false
        while not(any(norm(next_col)^2 == small_squares))
            % keep trying until the new col has a nice norm
            next_col = randi(rrange,[m,1]);
            proj_col = next_col;
            for jx = 1:ix-1
                proj_col = proj_col - (next_col'*A(:,jx))/(A(:,jx)'*A(:,jx))*A(:,jx);
            end
            next_col = proj_col;
        end
        A = [A next_col];
    end
    
    % A currently has orthogonal columns
    U = A;
    
    % now create the example
    A = A * (triu(randi([1,4],size),1) + eye(size));
    
    
    % scale the columns of A to be integers
    % scale columns of U to be orthonormal
    for ix = 1:n
        scaleD = 1;
        scaleN = 0;
        for jx = 1:m
            if abs(A(jx, ix)) > tol
                [num, denom] = rat(A(jx,ix));
                scaleD = lcm(scaleD, denom);
                scaleN = gcd(scaleN, num);
            end
        end
        % ix, scaleN, scaleD
        A(:, ix) = (scaleD/scaleN) * A(:, ix);
        
        % also scale U to be orthonormal
        U(:, ix) = U(:, ix)/norm(U(:, ix));
    end

end

Tags for this Thread

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.

Contact us