Error in comparing equality of two matrices

9 views (last 30 days)
N/A
N/A on 24 Dec 2021
Commented: Walter Roberson on 27 Dec 2021
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

Answers (2)

Chunru
Chunru on 24 Dec 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 Comments
Stephen23
Stephen23 on 27 Dec 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
Walter Roberson on 27 Dec 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.

Sign in to comment.


N/A
N/A on 27 Dec 2021
I had a problem using the last "if" statement in my code where I was not able to compare inv_RBA and transpose_RBA. I figured out this way to compare them both:
k = 0;
for m = 1:size(inv_RBA)
for n = 1:size(transpose_RBA)
if isequal(i,j)
continue
else
disp("Fail")
k = 1
end
end
end
if k==0
disp("Property satisfied")
end
  3 Comments
N/A
N/A on 27 Dec 2021
Hi, it was not entirely clear to me as I'm a beginner, but thanks for your feedback. Would you be able to recommend a better way to compare each element of these matrices and generate a result which says both are equal?
Walter Roberson
Walter Roberson on 27 Dec 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"

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!