isreal 实阵判断
Determine if all array elements are real numbers
判断阵列元素是否全部是实数
Syntax
语法
tf = isreal(A)
Description
描述
tf = isreal(A) returns logical false (0) if any element of array A has an imaginary component, even if the value of that component is 0. It returns logical true (1) otherwise.
Tf=isreal(A),如果阵列中有一个数值不是实数,则返回0,如果阵列中全部是实数,则返回1。
~isreal(x) returns logical true for arrays that have at least one element with an imaginary component. The value of that component can be 0.
~isreal(x)如果阵列中至少含有一个虚部则结果返回0。
Note If a is real, complex(a) returns a complex number whose imaginary component is 0, and isreal(complex(a)) returns false. In contrast, the addition a + 0i returns the real value a, and isreal(a + 0i) returns true.
注意:如果是实数,complex(a)返回一个复数,其虚部是0,并且isreal(complex(a))返回false。相反,加号a+0i返回的实数a,并且isreal(a+0i)返回true。
Because MATLAB supports complex arithmetic, certain of its functions can introduce significant imaginary components during the course of calculations that appear to be limited to real numbers. Thus, you should use isreal with discretion.
因为MATLAB支持复数运算,它的某些函数能够在计算实数的时候插入一些重要的虚部,这样,你应该慎重的使用isreal。
Examples
例如:
Example 1. These examples use isreal to detect the presence or absence of imaginary numbers in an array. Let
举例1.这些例子使用isreal去检测在一个阵列中虚部的存在或缺少。令
x = magic(3);
y = complex(x);
isreal(x) returns true because no element of x has an imaginary component.
Isreal(x)返回真因为x元素中没有一个虚部。
isreal(x)
ans =
1
isreal(y) returns false, because every element of x has an imaginary component, even though the value of the imaginary components is 0.
Isreal(y)返回假,因为每一个x的元素中有一个虚部,尽管虚部的值是0。
isreal(y)
ans =
0
This expression detects strictly real arrays, i.e., elements with 0-valued imaginary components are treated as real.
这个表达式检测的确实数阵列,i.e,带有0值的虚部作为实数处理的。
~any(imag(y(:)))
ans =
1
Example 2. Given the following cell array,
举例2.给定下列容器阵列,
C{1,1} = pi; % double
C{1,2} = 'John Doe'; % char array
C{1,3} = 2 + 4i; % complex double
C{1,4} = ispc; % logical
C{1,5} = magic(3) % double array
C{1,6} = complex(5,0) % complex double
C =
[3.1416] 'John Doe' [2.0000+ 4.0000i] [1] [3x3 double] [5]
isreal shows that all but C{1,3} and C{1,6} are real arrays.
isreal展示所有的/阵列中除了C{1,3}和C{1,6}都为实数。
for k = 1:6
x(k) = isreal(C{1,k});
end
x
x =
1 1 0 1 1 0