2

I am working on a MATLAB task which deals with stain removal.

What am I doing? I have been given a 4x4 matrix. I have then been told that it may well be orthogonal. I have to make it prove that the DCT matrix is actually orthogonal.

This is the given DCT matrix:

0.5000   0.5000   0.5000   0.5000   
0.6533   0.2706  -0.2706  -0.6533
0.5000  -0.5000  -0.5000   0.5000  
0.2706  -0.6533   0.6533  -0.2706

Here's the code:

function [U, C, G] = UFGDCT(N)
%
%   Compute the matrices for DCT (-?-)
%
%   U is the unitary "in-between" matrix
%   C is the matrix of the DCT
%   G is the inverse of F
%
    C = zeros(N);
    for row = 0:N-1
        for col = 0:N-1
            C(row+1, col+1) = cos(pi*row*(col+(1/2))/N);
        end
    end
    for cols = 0:N-1
        C(1,cols+1) = C(1,cols+1)/sqrt(2);
    end
    C = C*sqrt(2/N);
    U = C;
    G = C';
end

How can I do it in the simplest way? I have tried to search about finding orthogonality of a matrix, but didn't get the luch. I could not find anything that could be helpful.

RESULT:

mat*mat'
mat'*mat

ans =

    1.0000         0         0         0
         0    1.0001         0         0
         0         0    1.0000         0
         0         0         0    1.0001


ans =

    1.0000    0.0000   -0.0000   -0.0000
    0.0000    1.0000   -0.0000   -0.0000
   -0.0000   -0.0000    1.0000    0.0000
   -0.0000   -0.0000    0.0000    1.0000
Henrik.A
  • 31
  • 5
  • 1
    Please include text as [formatted text](/help/formatting) instead of an image. Please take the [tour], read [ask] and the [question checklist](//meta.stackoverflow.com/q/260648/843953). Welcome to Stack Overflow! – Pranav Hosangadi Dec 07 '20 at 23:02
  • Thanks for that :-) – Henrik.A Dec 07 '20 at 23:29
  • In case it helps, you can obtain that matrix simply as `dct(eye(N))` – Luis Mendo Dec 07 '20 at 23:57
  • The results you got show that it's orthogonal enough - subject to numerical errors. You got an identity matrix both times, and that's what you wanted. You can also do `norm(mat'*mat)-eye(4)` and `norm(mat*mat')-eye(4)` - you want to get small numbers both times, and you will. So all is good. – Kuba hasn't forgotten Monica Dec 08 '20 at 02:42

1 Answers1

2

If you just want to check the orthogonality, you can use mat*mat' and mat'*mat to see if it is an identity matrix

>> mat*mat'
ans =

   1.0000        0        0        0
        0   1.0001        0  -0.0000
        0        0   1.0000  -0.0000
        0  -0.0000  -0.0000   1.0001

>> mat'*mat
ans =

   1.0000e+00  -2.4018e-17  -3.7377e-18  -2.5250e-05
  -2.4018e-17   1.0000e+00  -2.5250e-05  -3.7377e-18
  -3.7377e-18  -2.5250e-05   1.0000e+00  -2.4018e-17
  -2.5250e-05  -3.7377e-18  -2.4018e-17   1.0000e+00

You will see that a tolerance should be set to determine if mat is orthogonal

>> mat*mat'-eye(4)
ans =

            0            0            0            0
            0   5.0500e-05            0  -3.7377e-18
            0            0            0  -2.7756e-17
            0  -3.7377e-18  -2.7756e-17   5.0500e-05

>> mat'*mat-eye(4)
ans =

   2.5250e-05  -2.4018e-17  -3.7377e-18  -2.5250e-05
  -2.4018e-17   2.5250e-05  -2.5250e-05  -3.7377e-18
  -3.7377e-18  -2.5250e-05   2.5250e-05  -2.4018e-17
  -2.5250e-05  -3.7377e-18  -2.4018e-17   2.5250e-05

Data

mat = [0.5000   0.5000   0.5000   0.5000   
0.6533   0.2706  -0.2706  -0.6533
0.5000  -0.5000  -0.5000   0.5000  
0.2706  -0.6533   0.6533  -0.2706];
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • 2
    You should use `'` instead of `.'` here, in case the matrix is complex – Luis Mendo Dec 07 '20 at 23:41
  • @LuisMendo That's true! Thanks for feedback – ThomasIsCoding Dec 07 '20 at 23:42
  • 1
    Also, note that the test you include is not enough. You should also check that the matrix is square (or compute `mat'*mat` in addition to `mat*mat'`) – Luis Mendo Dec 07 '20 at 23:44
  • Thanks alot @ThomasIsCoding, you made it as simple as possible. By the way, I have question: In terms of originality in a matrix, how should it be explained? Because, I know that in vectors that if two lines are orthogonal it means that they are perpendicular to each other. So I assume in the matrix; it's orthogonal because it has the same values in digonal, where the zeros are located around? – Henrik.A Dec 07 '20 at 23:48
  • @Henrik.A Perhaps this could help https://mathworld.wolfram.com/OrthogonalMatrix.html – ThomasIsCoding Dec 07 '20 at 23:49
  • @LuisMendo , ThomasIsCoding, but the thing is which code do I have to use to prove its orthogonality, with or without -eye(4)? Also, I am getting different results, can you please check? I've updated my thread. Is that correct? – Henrik.A Dec 08 '20 at 00:19
  • Just subtract the matrix product and the identity matrix, as Thomas does, and see if all the entries in the result are very small. Ideally they should be zero, but they may not be due to numerical inaccuracies with floating-point numbers – Luis Mendo Dec 08 '20 at 01:15
  • @LuisMendo While nearly always using `.'` over `'` is encouraged for purely structural changes, I think its usage here is beneficial since the formula does generalize over the complex numbers in the case of unitary matrices. Now if the domain of the linear operators is limited to be over purely the reals, I'd place that as a separate test and keep the general formula "clean" (opinion, of course). – TroyHaskin Dec 08 '20 at 02:23