y = ceil (x) ¶Return the smallest integer not less than x.
This is equivalent to rounding towards positive infinity.
If x is complex, return
ceil (real (x)) + ceil (imag (x)) * I.
ceil ([-2.7, 2.7])
⇒ -2 3
y = fix (x) ¶Truncate fractional portion of x and return the integer portion.
This is equivalent to rounding towards zero. If x is complex, return
fix (real (x)) + fix (imag (x)) * I.
fix ([-2.7, 2.7]) ⇒ -2 2
y = floor (x) ¶Return the largest integer not greater than x.
This is equivalent to rounding towards negative infinity. If x is
complex, return floor (real (x)) + floor (imag (x)) * I.
floor ([-2.7, 2.7])
⇒ -3 2
y = round (x) ¶Return the integer nearest to x.
If x is complex, return
round (real (x)) + round (imag (x)) * I. If there
are two nearest integers, return the one further away from zero.
round ([-2.7, 2.7])
⇒ -3 3
y = roundb (x) ¶Return the integer nearest to x. If there are two nearest integers, return the even one (banker’s rounding).
If x is complex,
return roundb (real (x)) + roundb (imag (x)) * I.
See also: round.
m = max (x) ¶m = max (x, [], dim) ¶m = max (x, [], vecdim) ¶m = max (x, [], "all") ¶m = max (x, [], nanflag) ¶[m, im] = max (…) ¶[m, im] = max (…, "linear") ¶m = max (x, y) ¶m = max (x, y, nanflag) ¶… = max (…, "ComparisonMethod", method) ¶Find maximum values in the array x.
If x is a vector, then max (x) returns the maximum value of
the elements in x.
If x is a matrix, then max (x) returns a row vector with
each element containing the maximum value of the corresponding column in
x.
If x is an array, then max (x) computes the maximum value
along the first non-singleton dimension of x.
The optional input dim specifies the dimension to operate on and must be
a positive integer. Specifying any singleton dimension of x, including
any dimension exceeding ndims (x), will return x.
Specifying multiple dimensions with input vecdim, a vector of
non-repeating dimensions, will operate along the array slice defined by
vecdim. If vecdim indexes all dimensions of x, then it is
equivalent to the option "all". Any dimension in vecdim greater
than ndims (x) is ignored.
Specifying the dimension as "all" will cause max to operate on
on all elements of x, and is equivalent to max (x(:)).
If called with two output arguments, max also returns the first index of
the maximum value(s) in x. The second output argument is only valid when
max operates on a single input array. Setting the "linear" flag
returns the linear index to the corresponding minimum values in x.
If called with two input arrays (x and y), max return the
pairwise maximum according to the rules for Broadcasting.
The optional variable nanflag specifies whether to include or exclude
NaN values from the calculation using any of the previously specified
input argument combinations. The default value for nanflag is
"omitnan" which ignores NaN values in the calculation. To
include NaN values, set the value of nanflag to
"includenan", in which case max will return NaN, if any
element along the operating dimension is NaN.
The optional "ComparisonMethod" paired argument specifies the
comparison method for numeric input and it applies to both one input and two
input arrays. method can take any of the following values:
'auto' : This is the default method, which compares elements byreal (x) when x is real, and by abs (x) when
x is complex.
'real' : Compares elements by real (x) when xis real or complex. For elements with equal real parts, a second comparison by
imag (x) is performed.
'abs' : Compares elements by abs (x) when xis real or complex. For elements with equal magnitude, a second comparison by
angle (x) in the interval [-pi,pi] is performed.
m = min (x) ¶m = min (x, [], dim) ¶m = min (x, [], vecdim) ¶m = min (x, [], "all") ¶m = min (x, [], nanflag) ¶[m, im] = min (…) ¶[m, im] = min (…, "linear") ¶m = min (x, y) ¶m = min (x, y, nanflag) ¶… = min (…, "ComparisonMethod", method) ¶Find minimum values in the array x.
If x is a vector, then min (x) returns the minimum value of
the elements in x.
If x is a matrix, then min (x) returns a row vector with
each element containing the minimum value of the corresponding column in
x.
If x is an array, then min (x) computes the minimum value
along the first non-singleton dimension of x.
The optional input dim specifies the dimension to operate on and must be
a positive integer. Specifying any singleton dimension of x, including
any dimension exceeding ndims (x), will return x.
Specifying multiple dimensions with input vecdim, a vector of
non-repeating dimensions, will operate along the array slice defined by
vecdim. If vecdim indexes all dimensions of x, then it is
equivalent to the option "all". Any dimension in vecdim greater
than ndims (x) is ignored.
Specifying the dimension as "all" will cause min to operate on
on all elements of x, and is equivalent to min (x(:)).
If called with two output arguments, min also returns the first index of
the minimum value(s) in x. The second output argument is only valid when
min operates on a single input array. Setting the "linear" flag
returns the linear index to the corresponding minimum values in x.
If called with two input arrays (x and y), min return the
pairwise minimum according to the rules for Broadcasting.
The optional variable nanflag specifies whether to include or exclude
NaN values from the calculation using any of the previously specified
input argument combinations. The default value for nanflag is
"omitnan" which ignores NaN values in the calculation. To
include NaN values, set the value of nanflag to
"includenan", in which case min will return NaN, if any
element along the operating dimension is NaN.
The optional "ComparisonMethod" paired argument specifies the
comparison method for numeric input and it applies to both one input and two
input arrays. method can take any of the following values:
'auto' : This is the default method, which compares elements byreal (x) when x is real, and by abs (x) when
x is complex.
'real' : Compares elements by real (x) when xis real or complex. For elements with equal real parts, a second comparison by
imag (x) is performed.
'abs' : Compares elements by abs (x) when xis real or complex. For elements with equal magnitude, a second comparison by
angle (x) in the interval [-pi,pi] is performed.
M = cummax (x) ¶m = cummax (x, dim) ¶m = cummax (x, vecdim) ¶m = cummax (x, "all") ¶m = cummax (…, direction) ¶m = cummax (x, nanflag) ¶[m, im] = cummax (…) ¶[m, im] = cummax (…, "linear") ¶… = cummax (…, "ComparisonMethod", method) ¶Return the cumulative maximum values from the array x.
If x is a vector, then cummax (x) returns a vector of the
same size with the cumulative maximum values of x.
If x is a matrix, then cummax (x) returns a matrix of the
same size with the cumulative maximum values along each column of x.
If x is an array, then cummax(x) returns an array of the
same size with the cumulative product along the first non-singleton dimension
of x.
The class of output y is the same as the class of input x, unless x is logical, in which case y is double.
The optional input dim specifies the dimension to operate on and must be
a positive integer. Specifying any singleton dimension in x, including
any dimension exceeding ndims (x), will return x.
Specifying multiple dimensions with input vecdim, a vector of
non-repeating dimensions, will operate along the array slice defined by
vecdim. If vecdim indexes all dimensions of x, then it is
equivalent to the option "all". Any dimension in vecdim greater
than ndims (x) is ignored.
Specifying the dimension as "all" will cause cummax to operate
on all elements of x, and is equivalent to cummax (x(:)).
The optional input direction specifies how the operating dimension is traversed and can take the following values:
"forward" (default)The cumulative maximum values are computed from beginning (index 1) to end along the operating dimension.
"reverse"The cumulative maximum values are computed from end to beginning along the operating dimension.
The optional variable nanflag specifies whether to include or exclude
NaN values from the calculation using any of the previously specified
input argument combinations. The default value for nanflag is
"omitnan" which ignores NaN values in the calculation. To
include NaN values, set the value of nanflag to
"includenan", in which case max will return NaN, if any
element along the operating dimension is NaN.
If called with two output arguments, cummax also returns the first index
of the maximum value(s) in x. Setting the "linear" flag returns
the linear index to the corresponding minimum values in x.
The optional "ComparisonMethod" paired argument specifies the
comparison method for numeric input and it applies to both one input and two
input arrays. method can take any of the following values:
'auto' : This is the default method, which compares elements byreal (x) when x is real, and by abs (x) when
x is complex.
'real' : Compares elements by real (x) when xis real or complex. For elements with equal real parts, a second comparison by
imag (x) is performed.
'abs' : Compares elements by abs (x) when xis real or complex. For elements with equal magnitude, a second comparison by
angle (x) in the interval [-pi,pi] is performed.
M = cummin (x) ¶m = cummin (x, dim) ¶m = cummin (x, vecdim) ¶m = cummin (x, "all") ¶m = cummin (…, direction) ¶m = cummin (x, nanflag) ¶[m, im] = cummin (…) ¶[m, im] = cummin (…, "linear") ¶… = cummin (…, "ComparisonMethod", method) ¶Return the cumulative minimum values from the array x.
If x is a vector, then cummin (x) returns a vector of the
same size with the cumulative minimum values of x.
If x is a matrix, then cummin (x) returns a matrix of the
same size with the cumulative minimum values along each column of x.
If x is an array, then cummin(x) returns an array of the
same size with the cumulative product along the first non-singleton dimension
of x.
The class of output y is the same as the class of input x, unless x is logical, in which case y is double.
The optional input dim specifies the dimension to operate on and must be
a positive integer. Specifying any singleton dimension in x, including
any dimension exceeding ndims (x), will return x.
Specifying multiple dimensions with input vecdim, a vector of
non-repeating dimensions, will operate along the array slice defined by
vecdim. If vecdim indexes all dimensions of x, then it is
equivalent to the option "all". Any dimension in vecdim greater
than ndims (x) is ignored.
Specifying the dimension as "all" will cause cummin to operate
on all elements of x, and is equivalent to cummin (x(:)).
The optional input direction specifies how the operating dimension is traversed and can take the following values:
"forward" (default)The cumulative minimum values are computed from beginning (index 1) to end along the operating dimension.
"reverse"The cumulative minimum values are computed from end to beginning along the operating dimension.
The optional variable nanflag specifies whether to include or exclude
NaN values from the calculation using any of the previously specified
input argument combinations. The default value for nanflag is
"omitnan" which ignores NaN values in the calculation. To
include NaN values, set the value of nanflag to
"includenan", in which case max will return NaN, if any
element along the operating dimension is NaN.
If called with two output arguments, cummin also returns the first index
of the minimum value(s) in x. Setting the "linear" flag returns
the linear index to the corresponding minimum values in x.
The optional "ComparisonMethod" paired argument specifies the
comparison method for numeric input and it applies to both one input and two
input arrays. method can take any of the following values:
'auto' : This is the default method, which compares elements byreal (x) when x is real, and by abs (x) when
x is complex.
'real' : Compares elements by real (x) when xis real or complex. For elements with equal real parts, a second comparison by
imag (x) is performed.
'abs' : Compares elements by abs (x) when xis real or complex. For elements with equal magnitude, a second comparison by
angle (x) in the interval [-pi,pi] is performed.
h = hypot (x, y) ¶h = hypot (x, y, z, …) ¶Compute the element-by-element square root of the sum of the squares of x and y.
This is equivalent to
sqrt (x.^2 + y.^2), but is calculated in a manner that
avoids overflows for large values of x or y.
hypot can also be called with more than 2 arguments; in this case,
the arguments are accumulated from left to right:
hypot (hypot (x, y), z) hypot (hypot (hypot (x, y), z), w), etc.
dx = gradient (m) ¶[dx, dy, dz, …] = gradient (m) ¶[…] = gradient (m, s) ¶[…] = gradient (m, sx, sy, sz, …) ¶[…] = gradient (f, x0) ¶[…] = gradient (f, x0, s) ¶[…] = gradient (f, x0, sx, sy, …) ¶Calculate the gradient of sampled data or a function.
d d d
grad F(x,y,z) = -- F(x,y,z) i + -- F(x,y,z) j + -- F(x,y,z) k
dx dy dz
If m is a vector, calculate the one-dimensional numerical gradient of m. If m is a matrix the gradient is calculated for each dimension. The return argument(s) are the estimated partial derivatives for each dimension at the specified sample points.
The default spacing of between data points is 1. A constant spacing between points can be specified with the s parameter. If s is a scalar, the single spacing value is used for all dimensions. Otherwise, separate values of the spacing can be supplied by the sx, … arguments. Scalar values specify an equidistant spacing. Vector values for the sx, … arguments specify the coordinate for that dimension. The length must match the respective dimension of m.
If the first argument f is a function handle, the gradient of the function is calculated for the points in x0. As with sampled data, the spacing values between the points from which the gradient is estimated can be set via the s or dx, dy, … arguments. By default a spacing of 1 is used, however this is normally overly large unless the function is very slowly varying, and it is often necessary to specify a smaller sample spacing.
Example: numerical gradient of cos (analytically = -sin)
gradient (@cos, pi/2, .1) ⇒ -0.9983 -sin (pi/2) ⇒ -1
Programming Notes: The value for interior data points is approximated using the central difference.
y'(i) = 1/2 * (y(i+1) - y(i-1)).
At boundary points a linear extrapolation is applied.
y'(1) = y(2) - y(1).
z = dot (x, y) ¶z = dot (x, y, dim) ¶Compute the dot product of two vectors.
If x and y are matrices, calculate the dot products along the first non-singleton dimension.
If the optional argument dim is given, calculate the dot products along this dimension.
Implementation Note: This is equivalent to
sum (conj (X) .* Y, dim), but avoids forming a
temporary array and is faster. When X and Y are column vectors,
the result is equivalent to X' * Y. Although, dot
is defined for integer arrays, the output may differ from the expected result
due to the limited range of integer objects.
See also: cross, divergence, tensorprod.
z = cross (x, y) ¶z = cross (x, y, dim) ¶Compute the vector cross product of two 3-dimensional vectors x and y.
If x and y are arrays, the cross product is applied along the first dimension with three elements.
The optional argument dim forces the cross product to be calculated along the specified dimension. An error will be produced if the specified dimension is not three elements in size.
In the case of a complex output, orthogonality of the output with respect to the inputs is also satisfied, and the condition
dot (conj (z), x) ≡ dot (conj (z), y) = 0
is met. dot (z, x) = 0 and
dot (z, y) = 0 will not hold. Also note that instead of
using the dot function, the inner product
z(:).' * x(:) ≡ z(:).' * y(:) = 0
will meet the orthogonality condition for vector input.
Example Code:
cross ([1, 1, 0], [0, 1, 1])
⇒
1 -1 1
cross (magic (3), eye (3), 2)
⇒
0 6 -1
-7 0 3
9 -4 0
See also: dot, curl, divergence, conj.
div = divergence (x, y, z, fx, fy, fz) ¶div = divergence (fx, fy, fz) ¶div = divergence (x, y, fx, fy) ¶div = divergence (fx, fy) ¶Calculate divergence of a vector field given by the arrays fx, fy, and fz or fx, fy respectively.
d d d
div F(x,y,z) = -- F(x,y,z) + -- F(x,y,z) + -- F(x,y,z)
dx dy dz
The coordinates of the vector field can be given by the arguments x, y, z or x, y respectively.
[cx, cy, cz, v] = curl (x, y, z, fx, fy, fz) ¶[cz, v] = curl (x, y, fx, fy) ¶[…] = curl (fx, fy, fz) ¶[…] = curl (fx, fy) ¶v = curl (…) ¶Calculate curl of vector field given by the arrays fx, fy, and fz or fx, fy respectively.
/ d d d d d d \
curl F(x,y,z) = | -- Fz - -- Fy, -- Fx - -- Fz, -- Fy - -- Fx |
\ dy dz dz dx dx dy /
The coordinates of the vector field can be given by the arguments x, y, z or x, y respectively. v calculates the scalar component of the angular velocity vector in direction of the z-axis for two-dimensional input. For three-dimensional input the scalar rotation is calculated at each grid point in direction of the vector field at that point.
See also: divergence, gradient, del2, cross.
L = del2 (M) ¶L = del2 (M, h) ¶L = del2 (M, dx, dy, …) ¶Calculate the discrete Laplace operator.
For a 2-dimensional matrix M(x, y) this is defined as
1 / d^2 d^2 \
L = --- * | --- M(x,y) + --- M(x,y) |
4 \ dx^2 dy^2 /
For N-dimensional arrays the sum in parentheses is expanded to include second derivatives over the additional higher dimensions.
The spacing between evaluation points may be defined by h, which is a scalar defining the equidistant spacing in all dimensions. Alternatively, the spacing in each dimension may be defined separately by dx, dy, etc. A scalar spacing argument defines equidistant spacing, whereas a vector argument can be used to specify variable spacing. The length of the spacing vectors must match the respective dimension of M. The default spacing value is 1.
Dimensions with fewer than 3 data points are skipped. Boundary points are calculated from the linear extrapolation of interior points.
Example: Second derivative of 2*x^3
f = @(x) 2*x.^3; dd = @(x) 12*x; x = 1:6; L = 4*del2 (f(x)); assert (L, dd (x));
f = factorial (n) ¶Return the factorial of n where n is a real non-negative integer.
If n is a scalar, this is equivalent to prod (1:n). For
vector or matrix arguments, return the factorial of each element in the
array.
For non-integers see the generalized factorial function gamma.
Note that the factorial function grows large quite quickly, and even
with double precision values overflow will occur if n > 171. For
such cases consider gammaln.
pf = factor (q) ¶[pf, n] = factor (q) ¶Return the prime factorization of q.
The prime factorization is defined as prod (pf) == q
where every element of pf is a prime number. If q == 1,
return 1. The output pf is of the same numeric class as the input.
With two output arguments, return the unique prime factors pf and
their multiplicities. That is,
prod (pf .^ n) == q.
Implementation Note: If the input q is single or double,
then it must not exceed the corresponding flintmax. For larger
inputs, cast them to uint64 if they’re less than 2^64:
factor (uint64 (18446744073709011493)) ⇒ 571111 761213 42431951
For even larger inputs, use sym if you have the Symbolic package
installed and loaded:
factor (sym ('9444733049654361449941'))
⇒ (sym)
1 1
1099511627689 ⋅8589934669
g = gcd (a1, a2, …) ¶[g, v1, …] = gcd (a1, a2, …) ¶Compute the greatest common divisor of a1, a2, ...
All arguments must be the same size or scalar. For arrays, the greatest common divisor is calculated for each element individually. All elements must be ordinary or Gaussian (complex) integers. Note that for Gaussian integers, the gcd is only unique up to a phase factor (multiplication by 1, -1, i, or -i), so an arbitrary greatest common divisor among the four possible is returned.
Optional return arguments v1, …, contain integer vectors such that,
g = v1 .* a1 + v2 .* a2 + ...
Example code:
gcd ([15, 9], [20, 18]) ⇒ 5 9
Programming tip: To find the GCD of all the elements of a single array, use
num2cell instead of nested calls or a loop:
x = [30 42 70 105]; # vector or array of inputs
gcd (num2cell (x) {:})
⇒ 1
l = lcm (x, y) ¶l = lcm (x, y, …) ¶Compute the least common multiple of x and y, or of the list of all arguments.
All inputs must be of the same size, or scalar. All elements must be real integer or Gaussian (complex) integer. For complex inputs, the result is unique only up to a phase factor (multiplication by +1, +i, -1, or -i), and one of the four is returned arbitrarily.
Example code:
lcm (5:8, 9:12) ⇒ 45 30 77 24
Programming tip: To find the LCM of all the elements of a single array, use
num2cell instead of nested calls or a loop:
x = 1:10; # vector or array of inputs
lcm (num2cell (x) {:})
⇒ 2520
r = rem (x, y) ¶Return the remainder of the division x / y.
The remainder is computed using the expression
x - y .* fix (x ./ y)
An error message is printed if the dimensions of the arguments do not agree, or if either argument is complex.
Programming Notes: When calculating with floating point numbers (double,
single), values within a few eps of an integer will be rounded to that
integer before computation for compatibility with MATLAB. Any floating
point integers greater than flintmax (2^53 for double) will not compute
correctly. For larger integer values convert the input to uint64 before
calling this function.
By convention,
rem (x, 0) = NaN if x is a floating point variable rem (x, 0) = 0 if x is an integer variable rem (x, y) returns a value with the signbit from x
For the opposite conventions see the mod function. In general,
rem is best when computing the remainder after division of two
positive numbers. For negative numbers, or when the values are
periodic, mod is a better choice.
See also: mod.
m = mod (x, y) ¶Compute the modulo of x and y.
Conceptually this is given by
x - y .* floor (x ./ y)
and is written such that the correct modulus is returned for integer types.
This function handles negative values correctly. That is,
mod (-1, 3) is 2, not -1, as rem (-1, 3) returns.
An error results if the dimensions of the arguments do not agree, or if either of the arguments is complex.
Programming Notes: When calculating with floating point numbers (double,
single), values within a few eps of an integer will be rounded to that
integer before computation for compatibility with MATLAB. Any floating
point integers greater than flintmax (2^53 for double) will not compute
correctly. For larger integer values convert the input to uint64 before
calling this function.
By convention,
mod (x, 0) = x mod (x, y) returns a value with the signbit from y
For the opposite conventions see the rem function. In general,
mod is a better choice than rem when any of the inputs are
negative numbers or when the values are periodic.
See also: rem.
p = primes (n) ¶Return all primes up to n.
The output data class (double, single, uint32, etc.) is the same as the input class of n. The algorithm used is the Sieve of Eratosthenes.
Note: For a specific number n of primes, call
list_primes (n). Alternatively, call
primes (n*log (k*n))(1:n) where k is
about 5 or 6. This works because the distance from one prime to the next is
proportional to the logarithm of the prime, on average. On integrating,
there are about n primes less than n * log (5*n).
See also: list_primes, isprime.
p = list_primes () ¶p = list_primes (n) ¶List the first n primes.
If n is unspecified, the first 25 primes are listed.
y = sign (x) ¶Compute the signum function.
This is defined as
-1, x < 0;
sign (x) = 0, x = 0;
1, x > 0.
For complex arguments, sign returns x ./ abs (x).
Note that sign (-0.0) is 0. Although IEEE 754 floating point
allows zero to be signed, 0.0 and -0.0 compare equal. If you must test
whether zero is signed, use the signbit function.
See also: signbit.
y = signbit (x) ¶Return logical true if the value of x has its sign bit set and false otherwise.
This behavior is consistent with the other logical functions. See Logical Values. The behavior differs from the C language function which returns nonzero if the sign bit is set.
This is not the same as x < 0.0, because IEEE 754 floating point
allows zero to be signed. The comparison -0.0 < 0.0 is false,
but signbit (-0.0) will return a nonzero value.
See also: sign.