Array Functions

diag

Creation of a diagonal matrix or extraction of the diagonal elements of a matrix.

Syntax

M = diag(v)
M = diag(v,k)
v = diag(M)
v = diag(M,k)

Description

With a vector input argument, diag(v) creates a square diagonal matrix whose main diagonal is given by v. With a second argument, the diagonal is moved by that amount in the upper right direction for positive values, and in the lower left direction for negative values.

With a matrix input argument, the main diagonal is extracted and returned as a column vector. A second argument can be used to specify another diagonal.

Examples

diag(1:3)
  1 0 0
  0 2 0
  0 0 3
diag(1:3,1)
  0 1 0 0
  0 0 2 0
  0 0 0 3
  0 0 0 0
M = magic(3)
M =
  8 1 6
  3 5 7
  4 9 2
diag(M)
  8
  5
  2
diag(M,1)
  1
  7

See also

tril, triu, trace

end

Last index of a matrix.

Syntax

v(end)
M(end,end)

Description

When it is used in an expression used as an index to access some elements of a matrix, end gives the index of the last element (line or column, depending of the context).

Examples

a = 1:5; a(end-1:end)
    4 5
a(end) = 99
  a =
    1 2 3 4 99

See also

operator (), size, length

eye

Identity matrix.

Syntax

M = eye(n)
M = eye(m,n)
M = eye([m,n])

Description

eye builds a matrix whose diagonal elements are 1 and other elements 0. The size of the matrix is specified by one integer for a square matrix, or two integers (either as two arguments or in a vector of two elements) for a rectangular matrix.

Examples

eye(3)
  1 0 0
  0 1 0
  0 0 1
eye(2, 3)
  1 0 0
  0 1 0

See also

ones, zeros, diag

find

Find the indices of the non-null elements of a matrix.

Syntax

ix = find(v)
[ix,jx] = find(M)
[ix,jx,x] = find(M)

Description

With one output argument, find(v) returns a vector containing the indices of the nonzero elements of v.

With two output arguments, find(M) returns two vectors containing the indices (row in the first output argument, column in the second output argument) of the nonzero elements of M.

With three output arguments, find(M) returns in addition in the third output argument the nonzero values themselves.

Examples

ix = find([1.2,0;0,3.6])
ix =
  1
  4
[ix,jx] = find([1.2,0;0,3.6])
ix =
  1
  2
jx =
  1
  2
[ix,jx,x] = find([1.2,0;0,3.6])
ix =
  1
  2
jx =
  1
  2
x =
  1.2
  3.6

See also

sort

fliplr

Flip a matrix or a list around its vertical axis.

Syntax

M2 = fliplr(M1)
list2 = fliplr(list1)

Description

fliplr(M1) gives a matrix M2 which has the same size as M1, but where all columns are placed in reverse order.

fliplr(list1) gives a list list2 which has the same length as list1, but where all top-level elements are placed in reverse order. Elements themselves are left unchanged.

Examples

fliplr([1,2;3,4])
  2 1
  4 3
fliplr({1, 'x', {1,2,3}})
  {{1,2,3}, 'x', 1}

See also

flipud, rot90, reshape

flipud

Flip a matrix upside-down.

Syntax

M2 = flipud(M1)

Description

flipud(M1) gives a matrix M2 which has the same size as M1, but where all lines are placed in reverse order.

Example

flipud([1,2;3,4])
  3 4
  1 2

See also

fliplr, rot90, reshape

ind2sub

Conversion from single index to row/column subscripts.

Syntax

(i, j) = ind2sub(size, ind)

Description

ind2sub(size,ind) gives the row and column of the element which would be retrieved from a matrix whose size is specified by size by the single index ind. size must be either a scalar for square matrices or a vector of two elements for rectangular matrices. ind can be a matrix; the result is calculated separately for each element and has the same size.

Example

M = [3, 6; 8, 9];
M(3)
  8
(i, j) = ind2sub(size(M), 3)
  i =
    2
  j =
    1
M(i, j)
  8

See also

sub2ind, size

isempty

Test for empty matrices or empty lists.

Syntax

b = isempty(M)
b = isempty(list)

Description

isempty(obj) gives true if obj is the empty array [], the empty string '', or the empty list {}, and false otherwise.

Examples

isempty([])
  1
isempty(0)
  0
isempty('')
  1
isempty({})
  1
isempty({{}})
  0

See also

size, length

length

Length of a vector or a list.

Syntax

n = length(v)
n = length(list)

Description

length(v) gives the length of vector v. length(M) gives the number of rows or the number of columns of matrix M, whichever is larger. length(list) gives the number of elements in a list.

Examples

length(1:5)
  5
length((1:5)')
  5
length(ones(2,3))
  3
length({1, 1:6, 'abc'})
  3
length({{}})
  1

See also

size, end

magic

Magic square.

Syntax

M = magic(n)

Description

A magic square is a square array of size n-by-n which contains each integer between 1 and n^2, and whose sum of each column and of each line is equal. magic(n) returns magic square of size n-by-n.

There is no 2-by-2 magic square. If the size is 2, the matrix [1,3;4,2] is returned instead.

Example

magic(3)
  8 1 6
  3 5 7
  4 9 2

See also

zeros, ones, eye, rand, randn

ones

Matrix containing only ones.

Syntax

M = ones(n)
M = ones(m,n)
M = ones([m,n])

Description

ones builds a matrix whose elements are 1. The size of the matrix is specified by one integer for a square matrix, or two integers (either as two arguments or in a vector of tow elements) for a rectangular matrix.

Example

ones(2,3)
  1 1 1
  1 1 1

See also

zeros, eye, rand, randn, repmat

rand

Uniformly-distributed random number.

Syntax

x = rand
M = rand(n)
M = rand(n, m)
M = rand([n, m])
rand('seed', s);

Description

rand builds a scalar pseudo-random number uniformly distributed between 0 and 1. The lower bound 0 may be reached, but the upper bound 1 is never. The current implementation is based on a scalar 64-bit seed, which theoretically allows 2^64 different numbers. This seed can be set with the syntax rand('seed', s), where s is a scalar or a vector of two components. rand('seed', s) returns the empty matrix [] as output argument. To discard it, the statement should be followed by a semicolon.

rand(n), rand(m,n) and rand([m,n]) return square or rectangular matrices whose elements are pseudo-random numbers uniformly distributed between 0 and 1.

Examples

rand
  0.2361
rand(1, 3)
  0.6679 0.8195 0.2786
rand('seed',0);
rand
  0.2361

See also

randn

randn

Normally-distributed random number

Syntax

x = randn
M = randn(n)
M = randn(n, m)
M = randn([n, m])
randn('seed', s);

Description

randn builds a scalar pseudo-random number chosen from a normal distribution with zero mean and unit variance. The current implementation is based on a scalar 64-bit seed, which theoretically allows 2^64 different numbers. This seed can be set with the syntax randn('seed', s), where s is a scalar or a vector of two components. The seed is not the same as the seed of rand. randn('seed', s) returns the empty matrix [] as output argument. To discard it, the statement should be followed by a semicolon.

randn(n), randn(m,n) and randn([m,n]) return square or rectangular matrices whose elements are pseudo-random numbers chosen from a normal distribution.

Examples

randn
  1.5927
randn(1, 3)
  0.7856 0.6489 -0.8141
randn('seed',0);
randn
  1.5927

See also

rand

repmat

Replicate a matrix.

Syntax

M2 = repmat(M1, n)
M2 = repmat(M1, m, n)
M2 = repmat(M1, [m, n])

Description

repmat creates a matrix with multiple copies of its first argument. It can be seen as an extended version of ones, where 1 is replaced by an arbitrary matrix. The number of copies is m in the vertical direction, and n in the horizontal direction. The type of the first argument (number, character or logical value) is preserved.

Examples

repmat([1,2;3,4],1,2)
  1 2 1 2
  3 4 3 4
repmat('abc',3)
  abcabcabc
  abcabcabc
  abcabcabc

See also

zeros, ones, operator :, kron

reshape

Rearrange the elements of a matrix to change its shape.

Syntax

M2 = reshape(M1, m, n)
M2 = reshape(M1, [m, n])

Description

reshape(M1,m,n) or reshape(M1,[m,n]) changes the dimensions of the matrix M1 so that the result has m rows and n columns. M1 must have m*n elements; read line-wise, both M1 and the result have the same elements.

Example

reshape([1,2,3;10,20,30], 3, 2)
  1  2
  3  10
  20 30

See also

operator ()

rot90

Matrix rotation.

Syntax

M2 = rot90(M1)
M2 = rot90(M1, k)

Description

rot90(M1) rotates the matrix M1 90 degrees counter-clockwise; the top left element of M1 becomes the bottom left element of M2.

In rot90(M1,k), the second argument is the number of times the matrix is rotated 90 degrees counter-clockwise. With k = 2, the matrix is rotated by 180 degrees; with k = 3 or k = -1, the matrix is rotated by 90 degrees clockwise.

Examples

rot90([1,2,3;4,5,6])
  3 6
  2 5
  1 4
rot90([1,2,3;4,5,6], -1)
  4 1
  5 2
  6 3
rot90([1,2,3;4,5,6], -1)
  6 5 4
  3 2 1
fliplr(flipud([1,2,3;4,5,6]))
  6 5 4
  3 2 1

See also

fliplr, flipud, reshape

size

Size of a matrix.

Syntax

v = size(M)
(m, n) = size(M)
m = size(M, 1)
n = size(M, 2)

Description

size(M) returns the number of rows and the number of columns of matrix M, either in a 1-by-2 vector or as two scalars if there are two output arguments.

size(M,1) gives the number of rows of matrix M; size(M,2) gives the number of columns.

Examples

M = ones(3, 5);
size(M)
  3 5
(m, n) = size(M)
  m =
    3
  n =
    5
size(M, 1)
  3
size(M, 2)
  5

See also

length

sort

Array sort.

Syntax

(M_sorted, ix) = sort(M)
(M_sorted, ix) = sort(M, dim)

Description

sort(M) sorts separately the elements of each column of the matrix M, or the elements of M if it is a row vector. The result has the same size as M. The optional second output argument gives the permutation matrix which transforms M into the sorted matrix. It can be used to reorder elements in another matrix or to sort the rows of a matrix with respect to one of its columns, as shown in the second example below. Order of identical elements is preserved.

If a second argument dim is provided, the sort is performed column by column if dim is 1, and row by row if it is 2.

Examples

sort([3,6,2,3,9,1,2])
  1 2 2 3 3 6 9
sort([2,5,3;1,4,2;6,1,1])
  1 1 1
  2 4 2
  6 5 3
M = [2,4; 5,1; 3,9; 4,0]
  2 4
  5 1
  3 9
  4 0
(Ms, ix) = sort(M(:,1));  % sort first column
M(ix,:)                   % reorder after the permutation vector
  2 4
  3 9
  4 0
  5 1

Algorithm

Shell sort.

sub2ind

Conversion from row/column subscripts to single index.

Syntax

ind = sub2ind(size, i, j)

Description

sub2ind(size,i,j) gives the single index which can be used to retrieve the element corresponding to the i:th row and the j:th column of a matrix whose size is specified by size. size must be either a scalar for square matrices or a vector of two elements for rectangular matrices. If i and j are matrices, they must have the same size: the result is calculated separately for each element and has the same size.

Example

M = [3, 6; 8, 9];
M(2, 1)
  8
sub2ind(size(M), 2, 1)
  7
M(3)
  8

See also

ind2sub, size

tril

Extraction of the lower triangular part of a matrix.

Syntax

L = tril(M)
L = tril(M,k)

Description

tril(M) extracts the lower triangular part of a matrix; the result is a matrix of the same size where all the elements above the main diagonal are set to zero. A second argument can be used to specify another diagonal: 0 is the main diagonal, positive values are above and negative values below.

Examples

M = magic(3)
M =
  8 1 6
  3 5 7
  4 9 2
tril(M)
  8 0 0
  3 5 0
  4 9 2
tril(M,1)
  8 1 0
  3 5 7
  4 9 2

See also

triu, diag

triu

Extraction of the upper triangular part of a matrix.

Syntax

U = triu(M)
U = triu(M,k)

Description

tril(M) extracts the upper triangular part of a matrix; the result is a matrix of the same size where all the elements below the main diagonal are set to zero. A second argument can be used to specify another diagonal; 0 is the main diagonal, positive values are above and negative values below.

Examples

M = magic(3)
M =
  8 1 6
  3 5 7
  4 9 2
triu(M)
  8 1 6
  0 5 7
  0 0 2
triu(M,1)
  0 1 6
  0 0 7
  0 0 0

See also

tril, diag

unique

Keep unique elements.

Syntax

v2 = unique(v1)

Description

unique(v1) sorts the elements of vector v2 and removes duplicate elements. Unless v1 is a row vector, the result is a column vector even if v1 is a matrix.

Example

unique([4,7,3,8,7,1,3])
  1 3 4 7 8

See also

sort

zeros

Null matrix.

Syntax

M = zeros(n)
M = zeros(m,n)
M = zeros([m,n])

Description

zeros builds a matrix whose elements are 0. The size of the matrix is specified by one integer for a square matrix, or two integers (either as two arguments or in a vector of tow elements) for a rectangular matrix.

Examples

zeros([2,3])
  0 0 0
  0 0 0
zeros(2)
  0 0
  0 0

See also

ones, eye, rand, randn, repmat


Copyright 1998-2001, Calerga.

All rights reserved.