필터 지우기
필터 지우기

Error in comparing equality of two matrices

조회 수: 6 (최근 30일)
N/A
N/A 2021년 12월 24일
댓글: Walter Roberson 2021년 12월 27일
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

답변 (2개)

Chunru
Chunru 2021년 12월 24일
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
Stephen23
Stephen23 2021년 12월 27일
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 2021년 12월 27일
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
N/A 2021년 12월 27일
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
N/A
N/A 2021년 12월 27일
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 2021년 12월 27일
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"

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by