complex

Creates a complex array from real and imaginary parts.

Category: Mathematics > Elementary Math

Syntax

z = complex(a, b)

Returns a + b*i, always stored as complex even if b is zero.

z = complex(a)

Returns a as a complex value with zero imaginary part.

Description

Arithmetic that produces a zero imaginary part normally returns a real number, so 1 + 0i is real. complex forces the complex type, so isreal(complex(1, 0)) is false. This matters for functions whose behavior depends on the type, and it lets a complex array keep its imaginary part when all values happen to be zero.

Input arguments

a, b
Real arrays of the same size, or scalars.

Output arguments

z
Complex array of the same size.

Example

Forcing a complex type

c = complex(1, 0)
isreal(c)
isreal(1 + 0i)
complex([1 2], [3 4])
Output
c = 1.0000 + 0.0000i
ans = logical
   0
ans = logical
   1
ans = 1×2 complex

   1.0000 + 3.0000i   2.0000 + 4.0000i

See also

real imag isreal conj