IF statements to compare Imaginary Numbers

4 visualizzazioni (ultimi 30 giorni)
Kayne
Kayne il 21 Ago 2011
Hi,
I have been trying to work out how to use a IF statement to see if two imarinary matrixs are the same, then display "results re correct' or soething like that. My code is below
EG,
clear
clc
%-------------------Setting Up the Variables-------------------------------
X = [1, -1]' ; % The Orignal Funcation
X1 = [0, -1 - 1i]' ; % The Recieved Function
% -------------------Potential transforms matries for W -------------------
W1 = 1/(sqrt(2))*[1,1;exp(0),-exp(0)] ; % Transform 1
W2 = 1/(sqrt(2))*[1,1;exp(1i*pi/4),-exp(1i*pi/4)] ; % Transform 2
W3 = 1/(sqrt(2))*[1,1;exp(1i*pi/2),-exp(1i*pi/2)] ; % Transform 3
W4 = 1/(sqrt(2))*[1,1;exp(3i*pi/4),-exp(3i*pi/4)]; % Transform 4
%-------------------Solving to find the Value for X1-----------------------
A = W1*X ; % solves for Transform 1 and the orignal funcation
B = W2*X ; % solves for Transform 2 and the orignal funcation
C = W3*X ; % solves for Transform 3 and the orignal funcation
D = W4*X ;% solves for Transform 4 and the orignal funcation
Now the if statements that I have been working on are as follows
if A==X1
disp ('Resutls are the same')
elseif B==X1
disp ('Resutls are the same')
elseif C==X1
disp ('Resutls are the same')
elseif D==X1
disp ('Resutls are the same')
end
Now I know that the answer is D but continue to get nothing for the result. I have read about this and it has something to do with floating point numbers and what Matlabs can display. I am a little lost as to how the code should look so that the answer D will display something saying it is the same.
Any help would be appriciated
Thanks Kayne

Risposte (2)

Paulo Silva
Paulo Silva il 21 Ago 2011
Do this
D==X1
ans=
1
0
It's really something about floating point, try doing this:
(D>=X1-eps & D<=X1+eps)
now replace all the comparisons on your code the same way and all should work.
Better explanation and ways to deal with the situation can be found here
  2 Commenti
Kayne
Kayne il 21 Ago 2011
Thanks for the reply Paulo,
Your solution worked and has helped me greatly. Now to get into the finner details and understand why it worked.
Thanks heaps for your time and help
Kind Regards,
Kayne
Paulo Silva
Paulo Silva il 21 Ago 2011
Do this: X1-D , it isn't zero but very close to it, very near eps.

Accedi per commentare.


Walter Roberson
Walter Roberson il 21 Ago 2011

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by