Error in comparing equality of two matrices
Show older comments
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
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
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
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.
N/A
on 27 Dec 2021
3 Comments
Walter Roberson
on 27 Dec 2021
That code does not define i or j so they will either be whatever was in the workspace, or else they will be the default values for those two names, which are functions that return sqrt(-1) .
Caution: size() of a variable, when no option is provided in the size() call, always returns a vector, even if the variable is a scalar. When you use 1:size() then you are using 1 colon a vector . MATLAB does define that: it takes the first element of the vector. So 1:size(inv_RBA) is going to be the same as 1:size(inv_RBA,1) which might not be what you want;.
N/A
on 27 Dec 2021
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"
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!