Logical with complex numbers in a vector

I have a complex number in a vector,
e =
-0.0000
3.0000
9.0000
>> logical(e) %checking if the the first entry is actually 0. Results show it is not
ans =
3×1 logical array
1
1
1
>> t = sqrt(e) % sqauring the entries shows all the three entries are complex numbers
t =
0.0000 + 0.0000i
1.7321 + 0.0000i
3.0000 + 0.0000i
L = logical(t) % In Matlab documentation it not possible to use logical for complex numbers
Error using logical
Complex values cannot be converted to logicals.
>> t(1,1)==0 % checking if the first entry is actually 0?
ans =
logical
0
>> t(1,1)<0 % checking if the first entry is actually less than 0?
ans =
logical
0
>> t(1,1)>0 % checking if the first entry is actually greater than 0?
ans =
logical
0
% QUESTION: is there a way to compare t(1,1) with 0 to know whether is it 0 or not?
% Alternatively: What is the genral way of using logicals for complex numbers?

 채택된 답변

Walter Roberson
Walter Roberson 2021년 2월 26일

0 개 추천

e = [-1e-9;3;9;0]
e = 4×1
-0.0000 3.0000 9.0000 0
t = sqrt(e);
[real(t), imag(t)]
ans = 4×2
0 0.0000 1.7321 0 3.0000 0 0 0
t == 0
ans = 4x1 logical array
0 0 0 1
format long g
e
e = 4×1
-1e-09 3 9 0
[real(t), imag(t)]
ans = 4×2
0 3.16227766016838e-05 1.73205080756888 0 3 0 0 0
You are being mislead by your default output format. If you switch output formats you will be able to see quickly that your e(1) is a small negative value.
You can see that t == 0 is enough to compare both parts of a complex number to 0, and correspondingly,
Lt = t ~= 0
Lt = 4x1 logical array
1 1 1 0
is the equivalent of logical()

추가 답변 (2개)

Image Analyst
Image Analyst 2021년 2월 27일

1 개 추천

Perhaps this:
complexVector =[
0.0000 + 0.1000i
1.7321 + 0.0010i
3.0000 + 0.0000i]
% Define a tolerance:
tolerance = 0.01
% Find out which elements have an imaginary magnitude less than tolerance:
belowTolerance = imag(complexVector) < tolerance
% For those elements with an imaginary value below the tolerance,
% turn them into a real number with no imaginary component.
complexVector(belowTolerance) = real(complexVector(belowTolerance))
You get:
complexVector =
0 + 0.1i
1.7321 + 0.001i
3 + 0i
tolerance =
0.01
belowTolerance =
3×1 logical array
0
1
1
complexVector =
0 + 0.1i
1.7321 + 0i
3 + 0i
Note the second and third elements are real, not complex, numbers because the imaginary part was stripped off.
David Hill
David Hill 2021년 2월 23일

0 개 추천

%depends on what you are trying to compare
real(t)==0;
imag(t)==0;
norm(t)==0;

댓글 수: 7

Hmm!
Hmm! 2021년 2월 23일
편집: Hmm! 2021년 2월 23일
>> real(t(1,1))==0
ans =
logical
1
>> imag(t(1,1))==0
ans =
logical
0
% thus, my question is still unanswered: is there a way to compare
% t(1,1) with 0 to know whether it is 0 or not? I am comapring the
% "whole" number i.e., both the real amd imaginary part with 0. If the
% real part is 0 and the imaginary part is not 0, can we then conclude
% that the complex number is 0? What will be the supporting reason(s)?
Stephen23
Stephen23 2021년 2월 24일
편집: Stephen23 2021년 2월 24일
"If the real part is 0 and the imaginary part is not 0, can we then conclude that the complex number is 0?"
The answer to that question is clearly "no", because the imaginary part is non-zero.
You seem to be under the mistaken impression that it is meaningful to compare complex numbers with non-complex numbers. Until you define what that means, the answer will be "no".
Image Analyst
Image Analyst 2021년 2월 26일
Let's take a step back and ask why you think you want to do that. What is your use case?
Let's say you had a general complex number a + bi. Now, how were those numbers generated in the first place (what do they mean), and under what cases of a and b would you want a true logical value and under what cases of a and b would you want a false logical variable?
Your E, U, V are not complex.
You can test value ~= 0 and that will work for complex values as well.
Hmm!
Hmm! 2021년 2월 27일
편집: Hmm! 2021년 2월 27일
I'm sorry, I mistakenly posted a different arguement from another platform, what I really want to do is with A = [-1 1 0; -1 0 1; 0 -1 1; -2 1 1];
e=eig(A'*A)
e =
-0.0000
3.0000
9.0000
sqrt(e)
ans =
0.0000 + 0.0000i
1.7321 + 0.0000i
3.0000 + 0.0000i
% Here comes my question that I would like to specify (strictly)
% a user tolerance = 0 or a corresponding zero value in
%the space of complex numbers so that whenever any comlplex
% number attains zero, then it is zero. This is because, a nonzero
% value (no matter how small it is) will be treated as zero which
% in effect is not zero which could lead to a misleading conclusion.
A = ([-1 1 0; -1 0 1; 0 -1 1; -2 1 1]);
e=eig(A'*A)
e = 3×1
-0.0000 3.0000 9.0000
f = sqrt(e);
format short
[real(e), imag(e)]
ans = 3×2
-0.0000 0 3.0000 0 9.0000 0
[real(f), imag(f)]
ans = 3×2
0 0.0000 1.7321 0 3.0000 0
f ~= 0
ans = 3x1 logical array
1 1 1
format long g
[real(e), imag(e)]
ans = 3×2
-1.06895079652153e-16 0 3 0 9 0
[real(f), imag(f)]
ans = 3×2
0 1.03390076725067e-08 1.73205080756888 0 3 0
f ~= 0
ans = 3x1 logical array
1 1 1
You can see from this that the first entry in e is truly not 0... it just looks that way when you have "format short" in effect.
You can also see that comparing ~= 0 is successful in detecting that the imaginary component is not 0, which is what you want.
As = sym(A)
As = 
es = eig(As'*As)
es = 
Working with symbolic values shows us that the ideal value for the first eigenvalue is exact 0; the -1e-16 is due to numeric round-off.
Hmm!
Hmm! 2021년 2월 27일
You're right.

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

제품

릴리스

R2020b

질문:

2021년 2월 23일

댓글:

2021년 3월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by