Converts the argument to a 32-bit complex floating point number. The syntax for its use is
y = complex(x)
where x
is an n
-dimensional numerical array. Conversion follows the general C rules. Note that both NaN
and Inf
in the real and imaginary parts are both preserved under type conversion.
The following piece of code demonstrates several uses of complex
. First, we convert from an integer (the argument is an integer because no decimal is present):
--> complex(200) ans = <complex> - size: [1 1] 200.00000 0.00000000 i
In the next example, a double precision argument is passed in (the presence of a decimal without the f
suffix implies double precision).
--> complex(400.0) ans = <complex> - size: [1 1] 400.00000 0.00000000 i
In the next example, a dcomplex argument is passed in.
--> complex(3.0+4.0*i) ans = <complex> - size: [1 1] 3.0000000 4.0000000 i
In the next example, a string argument is passed in. The string argument is converted into an integer array corresponding to the ASCII values of each character.
--> complex('he') ans = <complex> - size: [1 2] Columns 1 to 2 104.00000 0.00000000 i 101.00000 0.00000000 i
In the next example, the NaN
argument is converted.
--> complex(nan) ans = <complex> - size: [1 1] nan 0.00000000 i
In the last example, a cell-array is passed in. For cell-arrays and structure arrays, the result is an error.
--> complex({4}) Error: Cannot convert cell-arrays to any other type.