flag an error message when a scalar is complex

Hi,
I would like to include in my algorithm an error message where it will tell me that a scalar is complex. So for example I would like to implement in Matlab the following idea
if scalar == complex number
show error message
end
How I can implement this Matlab ?
Thanks

 채택된 답변

Nobel Mondal
Nobel Mondal 2015년 5월 31일

0 개 추천

if ~isreal(num)
error('This is a complex number.')
end

댓글 수: 2

msh
msh 2015년 5월 31일
편집: msh 2015년 5월 31일
Thanks for the quick response. One clarification. In the ~isreal(num) , num is the relevant scalar, correct? can this be also extended to matrices ?
Yes, num is the scalar which you want to check for.
For matrix:
>> A = [ 1 2 3; 4 5+6i 7];
>> isreal(A)
ans =
0
>> isreal(A(1,1))
ans =
1
>> isreal(A(2,2))
ans =
0
Hope this clear things up for you.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

msh
2015년 5월 31일

댓글:

2015년 5월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by