How to make datatype in 2 matrices equal

1 view (last 30 days)
Im new to Matlab, so this might be a pretty basic question, suggestions are highly appreciated.
So when I run the code that compares I with A*Ainv it says that the two are not equal, that is understandable cause the formatting on their numberformatting are different. Now what I could find on formatting on the web, was for formatting the display of the numbers and not how matlab handles these, which is what I need for the comparison. How can I get around this?:
First I create an inverse of matrix A like this:
Ainv = inv(A);
I created an identity matrix like this, trying to get it's datatype to match the datatype (number formatting) of matrix A:
I = eye(size(A),'like',A);
Also tried:
I = eye(size(A),'int8');
However as you see on the output of I and A*Ainv there is a problem cause the numbers in I are formatted with 1 and 0, while the numbers in A*Ainv are formatted like this -0.0000, 1.0000. I have these 2 matrices as a result:
disp ( I );
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
disp ( A*Ainv );
1.0000 -0.0000 -0.0000 0.0000 -0.0000
0.0000 1.0000 -0.0000 0 -0.0000
-0.0000 -0.0000 1.0000 0.0000 -0.0000
0 0 0 1.0000 -0.0000
0 0 -0.0000 0.0000 1.0000
Then I run this code to multiply the matrix A with the inverse of matrix A, which I compare to the identity matrix, and if equal should display the message "A*Ainv is equal to the identity matrix I.":
if true
if A*Ainv == I
disp('A*Ainv is equal to the identity matrix I.');
else
disp('A*Ainv do not equal the identity matrix I.');
The final resulting output is which is not what we wish:
A*Ainv do not equal the identity matrix I.
We wish this instead:
A*Ainv is equal to the identity matrix I.

Accepted Answer

dpb
dpb on 24 Feb 2015
Edited: dpb on 25 Feb 2015
Classic case of floating point rounding...see
for a discussion. Note in the following example...
>> A=rand(4); % a random array
>> Ai=inv(A); % its inverse
>> AAinv=A*Ai % the product looks kinda' like yours...
AAinv =
1.0000 0 0 -0.0000
-0.0000 1.0000 0 -0.0000
0.0000 0 1.0000 0
0.0000 0 -0.0000 1.0000
>> AAinv==1 % who are identically unity???
ans =
1 0 0 0
0 0 0 0
0 0 0 0
0 0 0 1
>> Ainv(2,2) % 2nd diagonal wasn't...
ans =
1.0000
>> AAinv(2,2)-1
ans =
-3.5527e-15
>>
Note the default display was to not show enough significant digits to see, but when subract 1 from the value the rounding is clear at roughly machine precision.
>> fprintf('%.15f\n',Ainv(2,2)) % as is internally
0.999999999999996
>>
To accomplish the goal of showing "almost equal" for the result of the multiplication you'll have to do something like
>> all(all(abs(AAinv-eye(4)<3*eps)))
ans =
1
>>
where I used 3*eps as the tolerance factor.
  2 Comments
Olga Sirley
Olga Sirley on 25 Feb 2015
Edited: Olga Sirley on 25 Feb 2015
thx for that, exactly what I was looking for. Can I just add that after the if under my if True?
dpb
dpb on 25 Feb 2015
"if True" is redundant; remove that clause and the matching "end" entirely.
Yes, the expression (or something similar) would be appropriate for the logical of whether the solution is "close enough" to exact to satisfy or not. Note that the absolute magnitude of the error will be dependent upon the magnitude of the values; you may want to scale by an appropriate measure to account for that effect.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!