Error in comparing equality of two matrices
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, I am trying to add a conditional statement which compares whether two matrices are equal and generates a result. But it fails everytime. Please find my code below:
alpha = input("Enter value of alpha: ")
beta = input("Enter the value of beta: ")
gamma = input("Enter the value of gamma: ")
R_BA = rotz(alpha*pi/180)*roty(beta*pi/180)*rotx(gamma*pi/180)
X = R_BA(:,1)
Y = R_BA(:,2)
Z = R_BA(:,3)
mod_X = X(1).^2 + X(2).^2 + X(3).^2
mod_Y = Y(1).^2 + Y(2).^2 + Y(3).^2
mod_Z = Z(1).^2 + Z(2).^2 + Z(3).^2
if int64(mod_X)==int64(mod_Y)==int64(mod_Z)
disp("X, Y and Z vectors are unit vectors")
else
disp("Unitary condition not satisfied")
end
if dot(X,Y) == dot(Y,Z) == dot(Z,X)
disp("Orthonormal condition satisfied")
else
disp("Orthonormal condition not satisfied")
end
inv_RBA = inv(R_BA) %inverse of rotation matrix
transpose_RBA = transpose(R_BA)
if isequal(inv_RBA,transpose_RBA)
disp("Property satisfied")
else
disp("Fail")
end
The code at the end checks if inv_RBA is equal to transpose_RBA. The two matrices have the same exact elements but I do not get a positive output.
Highly appreciate your help here.
Thanks
0 commentaires
Réponses (2)
Chunru
le 24 Déc 2021
Use "isequal" for matrix comparison and "&&" for logic and.
% if int64(mod_X)==int64(mod_Y)==int64(mod_Z)
if isequal(int64(mod_X), int64(mod_Y) && isequal(int64(mod_Y), int64(mod_Z))
...
end
The same applies to "if dot(X,Y) == dot(Y,Z) == dot(Z,X)" as well.
2 commentaires
Stephen23
le 27 Déc 2021
Note that ISEQUAL accepts any number of input arguments (not just two), so this simplifies to:
isequal(int64(mod_X), int64(mod_Y, int64(mod_Z))
Note that this is unlikely to be useful as it does not take into account the binary floating point error of those calculations.
It is not clear what hypothesis converting to INT64 (which rounds the values) is expected to confirm or reject.
Walter Roberson
le 27 Déc 2021
I suspect that they were thinking of typecast() to uint64 and then comparing those underlying representations. But that does not give any more utility than comparing double precision.
N/A
le 27 Déc 2021
3 commentaires
Walter Roberson
le 27 Déc 2021
all( abs(inv_RBA - transpose_RBA) < TOLERANCE )
where TOLERANCE is some value that you feel is "close enough" to equal. For example if the values are near 2, then you might feel that 1e-13 was "close enough"
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!