Library - stdlib

stdlib is a library which extends the native LME functions in the following areas:

The following statement makes available functions defined in stdlib:

use stdlib

linspace

Sequence of linearly-spaced elements.

Syntax

v = linspace(x1, x2)
v = linspace(x1, x2, n)

Description

linspace(x1,x2) produces a row vector of 50 values spaced linearly from x1 and x2 inclusive. With a third argument, linspace(x1,x2,n) produces a row vector of n values.

Examples

linspace(1,10)
  1.0000 1.0909 1.1818 ... 9.9091 10.0000
linspace(1,2,6)
  1.0  1.2  1.4  1.6  1.8  2.0

See also

logspace, operator :

logspace

Sequence of logarithmically-spaced elements.

Syntax

v = logspace(x1, x2)
v = logspace(x1, x2, n)

Description

logspace(x1,x2) produces a row vector of 50 values spaces logarithmically from 10^x1 and 10^x2 inclusive. With a third argument, logspace(x1,x2,n) produces a row vector of n values.

Example

logspace(1,10)
  1.0000 1.0235 1.0476 ... 9.5455 9.7701 10.0000

See also

linspace, operator :

meshgrid

Arrays of X-Y coordinates.

Syntax

(X, Y) = meshgrid(x, y)

Description

meshgrid(x,y) produces two arrays of x and y coordinates suitable for the evaluation of a function of two variables. The input argument x is copied to the rows of the first output argument, and the input argument y is copied to the columns of the second output argument, so that both arrays have the same size.

Example

(X, Y) = meshgrid(1:5, 2:4)
  X =
    1  2  3  4  5
    1  2  3  4  5
    1  2  3  4  5
  Y =
    2  2  2  2  2
    3  3  3  3  3
    4  4  4  4  4
Z = atan2(X, Y)
  Z =
    0.4636    0.7854    0.9828    1.1071    1.1903
    0.3218    0.5880    0.7854    0.9273    1.0304
    0.2450    0.4636    0.6435    0.7854    0.8961

See also

linspace, logspace

compan

Companion matrix.

Syntax

X = compan(pol)

Description

compan(pol) gives the companion matrix of polynomial pol, a square matrix whose eigenvalues are the roots of pol.

Example

compan([2,3,4,5])
  -1.5  -2.0  -2.5
   1.0   0.0   0.0
   0.0   1.0   0.0

See also

poly, eig

hankel

Hankel matrix.

Syntax

X = hankel(c, r)

Description

hankel(c,r) creates a Hankel matrix whose first column contains the elements of vector c and whose last row contains the elements of vector r. A Hankel matrix is a matrix whose antidiagonals have the same value. In case of conflict, the first element of r is ignored. The default value of r is a zero vector the same length as c.

Example

hankel(1:3, 3:8)
  1  2  3  4  5  6
  2  3  4  5  6  7
  3  4  5  6  7  8

See also

toeplitz, diag

toeplitz

Toeplitz matrix.

Syntax

X = toeplitz(c, r)

Description

toeplitz(c,r) creates a Toeplitz matrix whose first column contains the elements of vector c and whose first row contains the elements of vector r. A Toeplitz matrix is a matrix whose diagonals have the same value. In case of conflict, the first element of r is ignored. With one argument, toeplitz gives a symmetric square matrix.

Example

toeplitz(1:3, 1:5)
  1  2  3  4  5
  2  1  2  3  4
  3  2  1  2  3

See also

hankel, diag

acot

Inverse cotangent.

Syntax

y = acot(x)

Description

acot(x) gives the inverse cotangent of x, which is complex if x is.

See also

cot, acoth, cos

acoth

Inverse hyperbolic cotangent.

Syntax

y = acoth(x)

Description

acot(x) gives the inverse hyperbolic cotangent of x, which is complex if x is complex or is in the range (-1,1).

See also

coth, acot, atanh

acsc

Inverse cosecant.

Syntax

y = acsc(x)

Description

acsc(x) gives the inverse cosecant of x,

See also

csc, acsch, asin

acsch

Inverse hyperbolic cosecant.

Syntax

y = acsch(x)

Description

acsch(x) gives the inverse hyperbolic cosecant of x, which is complex if x is.

See also

csc, acsc, asinh

asec

Inverse secant.

Syntax

y = asec(x)

Description

asec(x) gives the inverse secant of x, which is complex if x is complex or is in the range (-1,1).

See also

sec, asech, acos

asech

Inverse hyperbolic secant.

Syntax

y = asech(x)

Description

asech(x) gives the inverse hyperbolic secant of x, which is complex if x is complex or strictly negative.

See also

sech, asec, acosh

cot

Cotangent.

Syntax

y = cot(x)

Description

cot(x) gives the cotangent of x, which is complex if x is.

See also

acot, coth, tan

coth

Hyperbolic cotangent.

Syntax

y = coth(x)

Description

coth(x) gives the hyperbolic cotangent of x, which is complex if x is.

See also

acoth, cot, tanh

csc

Cosecant.

Syntax

y = csc(x)

Description

csc(x) gives the cosecant of x, which is complex if x is.

See also

acsc, csch, sin

csch

Hyperbolic cosecant.

Syntax

y = csch(x)

Description

csch(x) gives the hyperbolic cosecant of x, which is complex if x is.

See also

acsch, csc, sinh

sec

Secant.

Syntax

y = sec(x)

Description

sec(x) gives the secant of x, which is complex if x is.

See also

asec, sech, cos

sech

Hyperbolic secant.

Syntax

y = sech(x)

Description

acot(x) gives the hyperbolic secant of x, which is complex if x is.

See also

asech, sec, cosh

log2

Base 2 logarithm.

Syntax

y = log2(x)

Description

log2(x) gives the base 2 logarithm of x, defined as log2(x)=log(x)/log(2). The result is complex if x is not real positive.

Example

log2([1, 2, 1024, 2000, -5])
  0  1  10  10.9658  2.3219+4.5324j

See also

log, log10

isreal

Test for a real number.

Syntax

b = isreal(x)

Description

isreal(x) is true if x is a real scalar or a matrix whose entries are all real.

Examples

isreal([2,5])
  1
isreal([2,3+2j])
  0
isreal(exp(pi*1j))
  1

See also

isnumeric

cross

Cross product.

Syntax

v3 = cross(v1, v2)
v3 = cross(v1, v2, dim)

Description

cross(v1,v2) gives the cross products of vectors v1 and v2. v1 and v2 must be row or columns vectors of three components, or matrices containing several such vectors. When there is ambiguity, a third argument dim may be used to specify the dimension of vectors: 1 for column vectors, or 2 for row vectors.

Examples

cross([1; 2; 3], [0; 0; 1])
  2
 -1
  0
cross([1, 2, 3], [4, 0, 0; 0, 2, 0], 2)
  0  12  -8
 -6   0   2

See also

operator *, det

isprime

Prime number test.

Syntax

b = isprime(n)

Description

isprime(n) returns true if n is a prime number, or false otherwise. If n is a matrix, the test is applied to each element and the result is a matrix the same size.

Examples

isprime(7)
  true
isprime([0, 2, 10])
  F T F

See also

primes, factor

primes

List of primes.

Syntax

v = primes(n)

Description

primes(n) gives a row vector which contains the primes up to n.

Example

primes(20)
  2  3  5  7 11 13 17 19

See also

isprime

factor

Prime factors.

Syntax

v = factor(n)

Description

factor(n) gives a row vector which contains the prime factors of n in ascending order. Multiple prime factors are repeated.

Example

factor(350)
  2  5  5  7

See also

isprime

gcd

Greatest common divisor.

Syntax

q = gcd(a, b)

Description

gcd(a,b) gives the greatest common divisor of integer numbers a and b.

Example

gcd(72, 56)
  8

See also

lcd, factor

lcm

Least common multiple.

Syntax

q = lcd(a, b)

Description

lcd(a,b) gives the least common multiple of integer numbers a and b.

Example

lcm(72, 56)
  504

See also

gcd, factor

perms

Array of permutations.

Syntax

M = perms(v)

Description

perm(v) gives an array whose rows are all the possible permutations of vector v.

Example

perms(1:3)
  3  2  1
  3  1  2
  2  3  1
  1  3  2
  2  1  3
  1  2  3

See also

sort

cart2pol

Cartesian to polar coordinates transform.

Syntax

(phi, r) = cart2pol(x, y)
(phi, r, z) = cart2pol(x, y, z)

Description

(phi,r)=cart2pol(x,y) transforms cartesian coordinates x and y to polar coordinates phi and r such that x=r*cos(phi) and x=r*sin(phi).

(phi,r,z)=cart2pol(x,y,z) transform cartesian coordinates to cylindrical coordinates, leaving z unchanged.

Example

(phi, r) = cart2pol(1, 2)
  phi =
    1.1071
  r =
    2.2361

See also

cart2sph, pol2cart, sph2cart

cart2sph

Cartesian to spherical coordinates transform.

Syntax

(phi, theta, r) = cart2sph(x, y, z)

Description

(phi,theta,r)=cart2sph(x,y,z) transforms cartesian coordinates x, y, and z to polar coordinates phi, theta, and r such that x=r*cos(phi)*cos(theta), x=r*sin(phi)*cos(theta), and z=r*sin(theta).

Example

(phi, theta, r) = cart2sph(1, 2, 3)
  phi =
    1.1071
  theta =
    0.9303
  r =
    3.7417

See also

cart2pol, pol2cart, sph2cart

pol2cart

Polar to cartesian coordinates transform.

Syntax

(x, y) = pol2cart(phi, r)
(x, y, r) = pol2cart(phi, r, z)

Description

(x,y)=pol2cart(phi,r) transforms polar coordinates phi and r to cartesian coordinates x and y such that x=r*cos(phi) and x=r*sin(phi).

(x,y,z)=pol2cart(phi,r,z) transforms cylindrical coordinates to cartesian coordinates, leaving z unchanged.

Example

(x, y) = pol2cart(1, 2)
  x =
    1.0806
  y =
    1.6829

See also

cart2pol, cart2sph, sph2cart

sph2cart

Spherical to cartesian coordinates transform.

Syntax

(x, y, z) = sph2cart(phi, theta, r)

Description

(x,y,z)=sph2cart(phi,theta,r) transforms polar coordinates phi, theta, and r to cartesian coordinates x, y, and z such that x=r*cos(phi)*cos(theta), x=r*sin(phi)*cos(theta), and z=r*sin(theta).

Example

(x, y, z) = sph2cart(1, 2, 3)
  x =
    -0.6745
  y =
    -1.0505
  z =
    2.7279

See also

cart2pol, cart2sph, pol2cart

median

Median.

Syntax

x = median(v)
v = median(M)
v = median(M, dim)

Description

median(v) gives the median of vector v, i.e. the value x such that half of the elements of v are smaller and half of the elements are larger.

median(M) gives a row vector which contains the median of the columns of M. With a second argument, median(M,dim) operates along dimension dim.

Example

median([1, 2, 5, 6, inf])
  5

See also

mean, sort

std

Standard deviation.

Syntax

x = std(v)
x = std(v, p)
v = std(M)
v = std(M, p)
v = std(M, p, dim)

Description

std(v) gives the standard deviation of vector v, normalized by length(v)-1. With a second argument, std(v,p) normalizes by length(v)-1 if p is true, or by length(v) if p is false.

std(M) gives a row vector which contains the standard deviation of the columns of M. With a third argument, median(M,p,dim) operates along dimension dim.

Example

std([1, 2, 5, 6, 10, 12])
  4.3359

See also

mean, cov

sortrows

Sort matrix rows.

Syntax

(S, index) = sortrows(M)
(S, index) = sortrows(M, sel)
(S, index) = sortrows(M, sel, dim)

Description

sortrows(M) sort the rows of matrix M. The sort order is based on the first column of M, then on the second one for rows with the same value in the first column, and so on.

sortrows(M,sel) use the columns specified in sel for comparing the rows of M. A third argument dim can be used to specify the dimension of the sort: 1 for sorting the rows, or 2 for sorting the columns.

The second output argument of sortrows gives the new order of the rows or columns as a vector of indices.

Example

sortrows([3, 1, 2; 2, 2, 1; 2, 1, 2])
  2  1  2
  2  2  1
  3  1  2

See also

sort

hist

Histogram.

Syntax

(N, X) = hist(Y)
(N, X) = hist(Y, m)
N = hist(Y, X)

Description

hist(Y) gives the number of elements of vector Y in 10 equally-spaced intervals. A second input argument may be used to specify the number of intervals. The center of the intervals may be obtained in a second output argument.

Example

(N, X) = hist(logspace(1,10), 5)
  N =
    45    21    14    11     9
  X =
     1.9   3.7   5.5   7.3   9.1

trapz

Numerical integration with trapezoidal approximation.

Syntax

s = trapz(Y)
s = trapz(X, Y)
s = trapz(X, Y, dim)

Description

trapz(Y) calculates an approximation of the integral of a function given by the samples in Y with unit intervals. The trapezoidal approximation is used. If Y is neither a row nor a column vector, integration is performed along its columns and the result is a row vector.

trapz(X,Y) specifies the location of the samples. A third argument may be used to specify along which dimension the integration is performed.

Example

trapz([2, 3, 5])
  6.5
trapz([1, 2, 5], [2, 3, 5])
  14.5

See also

sum, cumtrapz

cumtrapz

Cumulative numerical integration with trapezoidal approximation.

Syntax

S = cumtrapz(Y)
S = cumtrapz(X, Y)
S = cumtrapz(X, Y, dim)

Description

cumtrapz(Y) calculates an approximation of the cumulative integral of a function given by the samples in Y with unit intervals. The trapezoidal approximation is used. If Y is neither a row nor a column vector, integration is performed along its columns. The result has the same size as Y. The first value(s) is (are) 0.

cumtrapz(X,Y) specifies the location of the samples. A third argument may be used to specify along which dimension the integration is performed.

Example

cumtrapz([2, 3, 5])
  0     2.5   6.5
cumtrapz([1, 2, 5], [2, 3, 5])
  0     2.5  14.5

See also

cumsum, trapz

corrcoef

Correlation coefficients.

Syntax

S = corrcoef(X)
S = corrcoef(X1, X2)

Description

corrcoef(X) calculates the correlation coefficients of the columns of the m-by-n matrix X. The result is a square n-by-n matrix whose diagonal is 1.

corrcoef(X1,X2) calculates the correlation coefficients of X1 and X2 and returns a 2-by-2 matrix. It is equivalent to corrcoef([X1(:),X2(:)]).

Example

corrcoef([1, 3; 2, 5; 4, 4; 7, 10])
  1       0.8915
  0.8915  1
corrcoef(1:5, 5:-1:1)
  1  -1
 -1   1

See also

cov

filter2

Digital 2-d filtering of data.

Syntax

Y = filter2(X, F)
Y = filter2(X, F, shape)

Description

filter2(X,F) filters matrix X with kernel F with a 2-d convolution. The result has the same size as X.

An optional third argument is passed to conv2 to specify another method to handle the borders.

See also

filter, conv2

fft2

2-d Fast Fourier Transform.

Syntax

Y = fft2(X)
Y = fft2(X, n)
Y = fft2(X, m, n)

Description

fft2(X) computes the 2-d Fourier transform of matrix X. The Fast Fourier Transform algorithm is used if the numbers of rows and/or columns are powers of 2.

fft2(X,n) crops and/or pads with zeros matrix X to a square n-by-n matrix before computing the Fourier transform. fft2(X,n,m) produces an m-by-n rectangular matrix.

See also

fft, ifft2

ifft2

Inverse 2-d Fast Fourier Transform.

Syntax

Y = ifft2(X)
Y = ifft2(X, n)
Y = ifft2(X, m, n)

See also

ifft, fft2

fftshift

Shift DC frequency of FFT from beginning to center of spectrum.

Syntax

Y = fftshift(X)

Description

fftshift(X) shifts halves of vector (1-d) or matrix (2-d) X to move the DC component to the center. It should be used after fft or fft2.

See also

fft, fft2, ifftshift

ifftshift

Shift DC frequency of FFT from center to beginning of spectrum.

Syntax

Y = ifftshift(X)

Description

ifftshift(X) shifts halves of vector (1-d) or matrix (2-d) X to move the DC component from the center. It should be used before ifft or ifft2. It reverses the effect of fftshift.

See also

ifft, ifft2, fftshift

polyvalm

Value of a polynomial with square matrix argument.

Syntax

Y = polyvalm(pol, X)

Description

polyvalm(pol,X) evaluates the polynom given by the coefficients pol (in descending power order) with a square matrix argument.

Example

polyvalm([1,2,8],[2,1;0,1])
  16  5
   0 11

See also

polyval

polyfit

Polynomial fit.

Syntax

pol = polyfit(x, y, n)

Description

polyfit(x,y,n) calculates the polynom (given as a vector of descending power coefficients) of order n which best fits the points given by vectors x and y. The least-square algorithm is used.

Example

pol = polyfit(1:5, [2, 1, 4, 5, 2], 3)
  pol =
    -0.6667  5.5714 -12.7619  9.8000
polyval(pol, 1:5)
    1.9429  1.2286  3.6571  5.2286  1.9429

circshift

Shift the elements of a matrix in a circular way.

Syntax

B = circshift(A, shift_vert)
B = circshift(A, [shift_vert, shift_hor])

Description

circshift(A,sv) shifts the rows of matrix A downward by sv rows. The sv bottom rows of the input matrix become the sv top rows of the output matrix. sv may be negative to go the other way around.

circshift(A,[sv,sh]) shifts the rows of matrix A downward by sv rows, and its columns to the right by sh columns. The sv bottom rows of the input matrix become the sv top rows of the output matrix, and the sh rightmost columns become the sh leftmost columns.

See also

rotate, fliplr, flipud


Copyright 2001, Calerga.

All rights reserved.