How to make datatype in 2 matrices equal
Afficher commentaires plus anciens
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.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!