0

I want to write a code for the following algorithm using MATLAB.

 **Input:** An square reciprocal matrix T = (tij) for (i, j = 1, 2, . . . , n),
 **Output:** A square reciprocal matrix C = (cij) for (i, j = 1, 2, . . . , n);
    1: for i = 1; i < n; i ++ do
    2: for j = i ; j < n; j ++ do
    3: cij = sqrt(tij /tji );
    4: cji = 1/cij ;
    5: end for
    6: end for

I have the following matrix T:

T=[...
0.08 0.02 0.34 0.67;...
0.01 0.08 0.17 0.34;...
0.02 0.04 0.09 0.18;...
0.01 0.02 0.04 0.09]

The answer C I found on a paper is:

C = [...
     1  2    4  8;...
   0.50 1    2  4;...
   0.25 0.50 1  2;...
   0.13 0.25 0.50 1]

So far, I have tried the following code, but I am not certain about it. I couldn't find the exact answer C above. Any idea or help, please?

C=zeros(n,n);
for i = 1:n 
    for j = i:n            
            C(i,j) = sqrt(T(i,j)/T(j,i)); 
            C(j,i) = 1/C(i,j) ;  
           
    end
end
C;
Userabc
  • 201
  • 1
  • 9
  • How does the result differ? It seems that the code implements the algorithm. Maybe your expected result is incorrect? Maybe the algorithm is incorrect? – Cris Luengo Sep 13 '20 at 19:35
  • @Cris Luengo The result I got using my code is : C=[ 1.0000 1.9803 3.9943 7.8554; 0.5050 1.0000 2.0002 3.9996; 0.2504 0.5000 1.0000 1.9975; 0.1273 0.2500 0.5006 1.0000]; – Userabc Sep 13 '20 at 19:56
  • 1
    We are talking about Matlab, right? It is as easy as C = sqrt(T ./ T.') – Xiangrui Li Sep 13 '20 at 20:02
  • 1
    Looks like you’re close enough, you’re suffering from floating-point rounding errors. See here: https://stackoverflow.com/questions/686439/why-is-24-0000-not-equal-to-24-0000-in-matlab — your input matrix is likely not exact, probably rounded to two decimal digits for display. – Cris Luengo Sep 13 '20 at 20:55

0 Answers0