Array concatenation.
cat(dim, A1, A2, ...)
cat(dim,A1,A2,...) concatenates arrays A1, A2, etc. along dimension dim. Other dimensions must match. cat is a generalization of the comma and the semicolon inside brackets.
cat(2, [1,2;3,4], [5,6;7,8]) 1 2 5 6 3 4 7 8 cat(3, [1,2;3,4], [5,6;7,8]) 2x2x2 array (:,:,1) = 1 2 3 4 (:,:,2) = 5 6 7 8
operator [], operator ;, operator ,
Cell array of empty arrays.
C = cell(n) C = cell(n1,n2,...) C = cell([n1,n2,...])
cell builds a cell array whose elements are empty arrays []. The size of the cell array is specified by one integer for a square array, or several integers (either as separate arguments or in a vector) for a cell array of any size.
cell(2, 3) 2x3 cell array
Function evaluation for each cell of a cell array.
A = cellfun(fun, C) A = cell(fun, C, ...)
cellfun(fun,C) evaluates function fun for each cell of cell array C. Each evaluation must give a scalar result of numeric, logical, or character type; results are returned as a non-cell array the same size as C. First argument is a function reference, an inline function, or the name of a function as a string.
With more than two input arguments, cellfun calls function fun as feval(fun,C{i},other), where C{i} is each cell of C in turn, and other stands for the remaining arguments of cellfun.
cellfun differs from map in two ways: the result is a non-cell array, and remaining arguments of cellfun are provided directly to fun.
cellfun(@isempty, {1, ''; {}, ones(5)}) F T T F map(@isempty, {1, ''; {}, ones(5)}) 2x2 cell array cellfun(@size, {1, ''; {}, ones(5)}, 2) 1 0 0 5
Creation of a diagonal matrix or extraction of the diagonal elements of a matrix.
M = diag(v) M = diag(v,k) v = diag(M) v = diag(M,k)
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.
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
Identity matrix.
M = eye(n) M = eye(m,n) M = eye([m,n]) M = eye(..., type)
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.
An additional input argument can be used to specify the type of the result. It must be the string 'double', 'single', 'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', or 'uint64' (64-bit arrays are not supported on all platforms).
eye(3) 1 0 0 0 1 0 0 0 1 eye(2, 3) 1 0 0 0 1 0 eye(2, 'int8') 2x2 int8 array 1 0 0 1
Find the indices of the non-null elements of an array.
ix = find(v) [s1,s2] = find(M) [s1,s2,x] = find(M) ... = find(..., n) ... = find(..., n, dir)
With one output argument, find(v) returns a vector containing the indices of the nonzero elements of v. v can be an array of any dimension; the indices correspond to the internal storage ordering and can be used to access the elements with a single subscript.
With two output arguments, find(M) returns two vectors containing the subscripts (row in the first output argument, column in the second output argument) of the nonzero elements of 2-dim array M. To obtain subscripts for an array of higher dimension, you can convert the single output argument of find to subscripts with ind2sub.
With three output arguments, find(M) returns in addition the nonzero values themselves in the third output argument.
With a second input argument n, find limits the maximum number of elements found. It searches forward by default; with a third input argument dir, find gives the n first nonzero values if dir is 'first' or 'f', and the n last nonzero values if dir is 'last' or 'l'.
ix = find([1.2,0;0,3.6]) ix = 1 4 [s1,s2] = find([1.2,0;0,3.6]) s1 = 1 2 s2 = 1 2 [s1,s2,x] = find([1.2,0;0,3.6]) s1 = 1 2 s2 = 1 2 x = 1.2 3.6 A = rand(3) A = 0.5599 0.3074 0.5275 0.3309 0.8077 0.3666 0.7981 0.6424 0.6023 find(A > 0.7, 2, 'last') 7 5
Flip an array along any dimension.
B = flipdim(A, dim)
flipdim(A,dim) gives an array which has the same size as A, but where indices of dimension dim are reversed.
flipdim(cat(3, [1,2;3,4], [5,6;7,8]), 3) 2x2x2 array (:,:,1) = 5 6 7 8 (:,:,2) = 1 2 3 4
fliplr, flipud, rot90, reshape
Flip an array or a list around its vertical axis.
A2 = fliplr(A1) list2 = fliplr(list1)
fliplr(A1) gives an array A2 which has the same size as A1, 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.
fliplr([1,2;3,4]) 2 1 4 3 fliplr({1, 'x', {1,2,3}}) {{1,2,3}, 'x', 1}
flipud, flipdim, rot90, reshape
Flip an array upside-down.
A2 = flipud(A1)
flipud(A1) gives an array A2 which has the same size as A1, but where all lines are placed in reverse order.
flipud([1,2;3,4]) 3 4 1 2
fliplr, flipdim, rot90, reshape
Conversion from single index to row/column subscripts.
(i, j, ...) = ind2sub(size, ind)
ind2sub(size,ind) gives the subscripts of the element which would be retrieved from an array 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 or more for arrays. ind can be an array; the result is calculated separately for each element and has the same size.
M = [3, 6; 8, 9]; M(3) 8 (i, j) = ind2sub(size(M), 3) i = 2 j = 1 M(i, j) 8
Interpolation.
Vi = interpn(x1, ..., xn, V, xi1, ..., xin) Vi = interpn(x1, ..., xn, V, xi1, ..., xin, method)
interpn(x1,...,xn,V,xi1,...,xin) interpolates data in a space of n dimensions. Input data are defined by array V, where element V(i,j,...) corresponds to coordinates x1(i), x2(j), etc. Interpolation is performed for each coordinates defined by arrays xi1, xi2, etc., which must all have the same size; the result is an array of the same size.
Length of vectors x1, x2, ... must match the size of V along the corresponding dimension. Vectors x1, x2, ... must be sorted (monotonically increasing or decreasing), but they do not have to be spaced uniformly. Interpolated points outside the input volume are set to nan. Input and output data can be complex. Imaginary parts of coordinates are ignored.
The default interpolation method is multilinear. An additional input argument can be provided to specify it with a string (only the first character is considered):
Argument | Meaning |
---|---|
'0' or 'nearest' | nearest value |
'<' | lower coordinates |
'>' | higher coordinates |
'1' or 'linear' | multilinear |
Method '<' takes the sample where each coordinate has its index as large as possible, lower or equal to the interpolated value, and smaller than the last coordinate. Method '>' takes the sample where each coordinate has its index greater or equal to the interpolated value.
One-dimension interpolation:
interpn([1, 2, 5, 8], [0.1, 0.2, 0.5, 1], [0, 2, 3, 7]) nan 0.2000 0.3000 0.8333 interpn([1, 2, 5, 8], [0.1, 0.2, 0.5, 1], [0, 2, 3, 7], '0') nan 0.2000 0.2000 1.0000
Three-dimension interpolation:
D = cat(3,[0,1;2,3],[4,5;6,7]); interpn([0,1], [0,1], [0,1], D, 0.2, 0.7, 0.5) 3.1000
Image rotation (we define original coordinates between -0.5 and 0.5 in vector c and arrays X and Y, and the image as a linear gradient between 0 and 1):
c = -0.5:0.01:0.5; X = repmat(c, 101, 1); Y = X'; phi = 0.2; Xi = cos(phi) * X - sin(phi) * Y; Yi = sin(phi) * X + cos(phi) * Y; D = 0.5 + X; E = interpn(c, c, D, Xi, Yi); E(isnan(E)) = 0.5;
Set intersection.
c = intersect(a, b) (c, ia, ib) = intersect(a, b)
intersect(a,b) gives the intersection of sets a and b, i.e. it gives the set of members of both sets a and b. Sets are any type of numerical, character or logical arrays, or lists or cell arrays of character strings. Multiple elements of input arguments are considered as single members; the result is always sorted and has unique elements.
The optional second and third output arguments are vectors of indices such that if (c,ia,ib)=intersect(a,b), then c is a(ia) as well as b(ib).
a = {'a','bc','bbb','de'}; b = {'z','bc','aa','bbb'}; (c, ia, ib) = intersect(a, b) c = {'bbb','bc'} ia = 3 2 ib = 4 2 a(ia) {'bbb','bc'} b(ib) {'bbb','bc'}
Set exclusive or can also be computed as the union of a and b minus the intersection of a and b:
setdiff(union(a, b), intersect(a, b)) {'a','aa','de','z'}
unique, union, setdiff, setxor, ismember
Inverse permutation of the dimensions of an array.
B = ipermute(A, perm)
ipermute(A,perm) returns an array with the same elements as A, but where dimensions are permuted according to the vector of dimensions perm. It performs the inverse permutation of permute. perm must contain integers from 1 to n; dimension perm(i) in A becomes dimension i in the result.
size(permute(rand(3,4,5), [2,3,1])) 5 3 4
Test for empty matrices or empty lists.
b = isempty(M) b = isempty(list)
isempty(obj) gives true if obj is the empty array [], the empty string '', or the empty list {}, and false otherwise.
isempty([]) true isempty(0) false isempty('') true isempty({}) true isempty({{}}) false
Test for cell arrays.
b = iscell(X)
iscell(X) gives true if X is a cell array or a list, and false otherwise.
iscell({1;2}) true iscell({1,2}) true islist({1;2}) false
Test for set membership.
b = ismember(m, s)
ismember(m,s) tests if elements of array m are members of set s. The result is a logical array the same size as m; each element is true if the corresponding element of m is a member of s, or false otherwise. m must be a numerical array or a cell array, matching type of set s.
s = {'a','bc','bbb','de'}; m = {'d','a','x';'de','a','z'}; b = ismember(m, s) b = F T F T T F
intersect, union, setdiff, setxor
Length of a vector or a list.
n = length(v) n = length(list)
length(v) gives the length of vector v. length(A) gives the number of elements along the largest dimension of array A. length(list) gives the number of elements in a list.
length(1:5) 5 length((1:5)') 5 length(ones(2,3)) 3 length({1, 1:6, 'abc'}) 3 length({{}}) 1
Magic square.
M = magic(n)
A magic square is a square array of size n-by-n which contains each integer
between 1 and
There is no 2-by-2 magic square. If the size is 2, the matrix [1,3;4,2] is returned instead.
magic(3) 8 1 6 3 5 7 4 9 2
Arrays of X-Y coordinates.
(X, Y) = meshgrid(x, y) (X, Y) = meshgrid(x)
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. meshgrid(x) is equivalent to meshgrid(x,x).
(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
Arrays of N-dimension coordinates.
(X1, ..., Xn) = ndgrid(x1, ..., xn) (X1, ..., Xn) = ndgrid(x)
ndgrid(x1,...,xn) produces n arrays of n dimensions. Array i is obtained by reshaping input argument i as a vector along dimension i and replicating it along all other dimensions to match the length of other input vectors. All output arguments have the same size.
With one input argument, ndgrid reuses it to match the number of output arguments.
(Y,X)=ndgrid(y,x) is equivalent to (X,Y)=meshgrid(x,y).
(X1, X2) = ndgrid(1:3) X1 = 1 1 1 2 2 2 3 3 3 X2 = 1 2 3 1 2 3 1 2 3
Number of dimensions of an array.
n = ndims(A)
ndims(A) returns the number of dimensions of array A, which is at least 2. Scalars, row and column vectors, and matrices have 2 dimensions.
ndims(magic(3)) 2 ndims(rand(3,4,5)) 3
size, squeeze, permute, ipermute
Number of nonzero elements.
n = nnz(A)
nnz(A) returns the number of nonzero elements of array A.
Conversion from numeric array to cell array.
C = num2cell(A) C = num2cell(A, dims)
num2cell(A) creates a cell array the same size as numeric array A. The value of each cell is the corresponding elements of A.
num2cell(A,dims) cuts array A along dimensions dims and creates a cell array with the result. Dimensions of cell array are the same as dimensions of A for dimensions not in dims, and 1 for dimensions in dims; dimensions of cells are the same as dimensions of A for dimensions in dims, and 1 for dimensions not in dims.
Argument A can be a numerical array of any dimension and class, a logical array, or a char array.
num2cell([1, 2; 3, 4]) {1, 2; 3, 4} num2cell([1, 2; 3, 4], 1) {[1; 3], [2; 4]} num2cell([1, 2; 3, 4], 2) {[1, 2]; [3, 4]}
Number of elements of an array.
n = numel(A)
numel(A) gives the number of elements of array A. It is equivalent to prod(size(A)).
numel(1:5) 5 numel(ones(2, 3)) 6 numel({1, 1:6; 'abc', []}) 4 numel({2, 'vwxyz'}) 2
Array of ones.
A = ones(n) A = ones(n1, n2, ...) A = ones([n1, n2, ...]) A = ones(..., type)
ones builds an array whose elements are 1. The size of the array is specified by one integer for a square matrix, or several integers (either as separate arguments or in a vector) for an array of any size.
An additional input argument can be used to specify the type of the result. It must be the string 'double', 'single', 'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', or 'uint64' (64-bit arrays are not supported on all platforms).
ones(2,3) 1 1 1 1 1 1 ones(2, 'int32') 2x2 int32 array 1 1 1 1
zeros, eye, rand, randn, repmat
Permutation of the dimensions of an array.
B = permute(A, perm)
permute(A,perm) returns an array with the same elements as A, but where dimensions are permuted according to the vector of dimensions perm. It is a generalization of the matrix transpose operator. perm must contain integers from 1 to n; dimension i in A becomes dimension perm(i) in the result.
size(permute(rand(3,4,5), [2,3,1])) 4 5 3
ndims, squeeze, ipermute, num2cell
Uniformly-distributed random number.
x = rand M = rand(n) M = rand(n1, n2, ...) M = rand([n1, n2, ...]) rand('seed', s);
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 arguments rand('seed', s), where s is a scalar or a vector of two components. rand('seed', s) returns the empty array [] as output argument. To discard it, the statement should be followed by a semicolon.
rand(n), rand(n1,n2,...) and rand([n1,n2,...]) return an n-by-n square matrix or an array of arbitrary size whose elements are pseudo-random numbers uniformly distributed between 0 and 1.
rand 0.2361 rand(1, 3) 0.6679 0.8195 0.2786 rand('seed',0); rand 0.2361
Normally-distributed random number
x = randn M = randn(n) M = randn(n1, n2, ...) M = randn([n1, n2, ...]) randn('seed', s);
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 arguments 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 array [] as output argument. To discard it, the statement should be followed by a semicolon.
randn(n), randn(n1,n2,...) and randn([n1,n2,...]) return an n-by-n square matrix or an array of arbitrary size whose elements are pseudo-random numbers chosen from a normal distribution.
randn 1.5927 randn(1, 3) 0.7856 0.6489 -0.8141 randn('seed',0); randn 1.5927
Replicate an array.
A2 = repmat(A1, n) A2 = repmat(A1, m, n) A2 = repmat(A1, [n1,...])
repmat creates an array with multiple copies of its first argument. It can be seen as an extended version of ones, where 1 is replaced by an arbitrary array. 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. With a vector as second argument, the array can be replicated along more than two dimensions.
repmat([1,2;3,4],1,2) 1 2 1 2 3 4 3 4 repmat('abc',3) abcabcabc abcabcabc abcabcabc
zeros, ones, operator :, kron, replist
Rearrange the elements of an array to change its shape.
A2 = reshape(A1) A2 = reshape(A1, n1, n2, ...) A2 = reshape(A1, [n1, n2, ...])
reshape(A1) gives a column vector with all the elements of array A1, which is read row-wise. If A1 is a variable, reshape(A1) is the same as A1(:).
reshape(A1,n1,n2,...) or reshape(A1,[n1,n2,...]) changes the dimensions of array A1 so that the result has m rows and n columns. A1 must have n1*n2*... elements; read line-wise, both A1 and the result have the same elements.
When dimensions are given as separate elements, one of them can be replaced with the empty array []; it is replaced by the value such that the number of elements of the result matches the size of input array.
reshape([1,2,3;10,20,30], 3, 2) 1 2 3 10 20 30 reshape(1:12, 3, []) 1 2 3 4 5 6 7 8 9 10 11 12
Array rotation.
A2 = rot90(A1) A2 = rot90(A1, k)
rot90(A1) rotates array A1 90 degrees counter-clockwise; the top left element of A1 becomes the bottom left element of A2. If A1 is an array with more than two dimensions, each plane corresponding to the first two dimensions is rotated.
In rot90(A1,k), the second argument is the number of times the array is rotated 90 degrees counter-clockwise. With k = 2, the array is rotated by 180 degrees; with k = 3 or k = -1, the array is rotated by 90 degrees clockwise.
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
Set difference.
c = setdiff(a, b) (c, ia) = setdiff(a, b)
setdiff(a,b) gives the difference between sets a and b, i.e. the set of members of set a which do not belong to b. Sets are any type of numerical, character or logical arrays, or lists or cell arrays of character strings. Multiple elements of input arguments are considered as single members; the result is always sorted and has unique elements.
The optional second output argument is a vector of indices such that if (c,ia)=setdiff(a,b), then c is a(ia).
a = {'a','bc','bbb','de'}; b = {'z','bc','aa','bbb'}; (c, ia) = setdiff(a, b) c = {'a','de'} ia = 1 4 a(ia) {'a','de'}
unique, union, intersect, setxor, ismember
Set exclusive or.
c = setxor(a, b) (c, ia, ib) = setxor(a, b)
setxor(a,b) performs an exclusive or operation between sets a and b, i.e. it gives the set of members of sets a and b which are not members of the intersection of a and b. Sets are any type of numerical, character or logical arrays, or lists or cell arrays of character strings. Multiple elements of input arguments are considered as single members; the result is always sorted and has unique elements.
The optional second and third output arguments are vectors of indices such that if (c,ia,ib)=setxor(a,b), then c is the union of a(ia) and b(ib).
a = {'a','bc','bbb','de'}; b = {'z','bc','aa','bbb'}; (c, ia, ib) = setxor(a, b) c = {'a','aa','de','z'} ia = 1 4 ib = 3 1 union(a(ia),b(ib)) {'a','aa','de','z'}
Set exclusive or can also be computed as the union of a and b minus the intersection of a and b:
setdiff(union(a, b), intersect(a, b)) {'a','aa','de','z'}
unique, union, intersect, setdiff, ismember
Size of an array.
v = size(A) (m, n) = size(A) m = size(A, i)
size(A) returns the number of rows and the number of elements along each dimension of array A, either in a row vector or as scalars if there are two output arguments or more.
size(A,i) gives the number of elements in array A along dimension i: size(A,1) gives the number of rows and size(A,2) the number of columns.
M = ones(3, 5); size(M) 3 5 (m, n) = size(M) m = 3 n = 5 size(M, 1) 3 size(M, 2) 5
Array sort.
(A_sorted, ix) = sort(A) (A_sorted, ix) = sort(A, dim) (A_sorted, ix) = sort(A, dir) (A_sorted, ix) = sort(A, dim, dir) (list_sorted, ix) = sort(list) (list_sorted, ix) = sort(list, dir)
sort(A) sorts separately the elements of each column of array A, or the elements of A if it is a row vector. The result has the same size as A. Elements are sorted in ascending order, with NaNs at the end. For complex arrays, numbers are sorted by magnitude.
The optional second output argument gives the permutation array which transforms A into the sorted array. It can be used to reorder elements in another array or to sort the rows of a matrix with respect to one of its columns, as shown in the last example below. Order of consecutive identical elements is preserved.
If a second numeric argument dim is provided, the sort is performed along dimension dim (columns if dim is 1, rows if 2, etc.)
An additional argument can specify the ordering direction. It must be the string 'ascending' (or 'a') for ascending order, or 'descending' (or 'd') for descending order. In both cases, NaNs are moved to the end.
sort(list) sorts the elements of a list, which must be strings. Cell arrays are sorted like lists, not column-wise like numeric arrays. The second output argument is a row vector. The direction can be specified with a second input argument.
sort([3,6,2,3,9,1,2]) 1 2 2 3 3 6 9 sort([2,5,3;nan,4,2;6,1,1]) 2 1 1 6 4 2 nan 5 3 sort([2,5,3;nan,4,2;6,1,1], 'd') 6 5 3 2 4 2 nan 1 1 sort({'def', 'abcd', 'abc'}) {'abc', 'abcd', 'def'}
To sort the rows of an array after the first column, one can obtain the permutation vector by sorting the first column, and use it as subscripts on the array rows:
M = [2,4; 5,1; 3,9; 4,0] 2 4 5 1 3 9 4 0 (Ms, ix) = sort(M(:,1)); M(ix,:) 2 4 3 9 4 0 5 1
Shell sort.
Suppression of singleton dimensions of an array.
B = squeeze(A)
squeeze(A) returns an array with the same elements as A, but where dimensions equal to 1 are removed. The result has at least 2 dimensions; row and column vectors keep their dimensions.
size(squeeze(rand(1,2,3,1,4))) 2 3 4 size(squeeze(1:5)) 1 5
Conversion from row/column subscripts to single index.
ind = sub2ind(size, i, j)
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 an array whose size is specified by size. size must be either a scalar for square matrices or a vector of two elements or more for other arrays. If i and j are arrays, they must have the same size: the result is calculated separately for each element and has the same size.
M = [3, 6; 8, 9]; M(2, 1) 8 sub2ind(size(M), 2, 1) 7 M(3) 8
Extraction of the lower triangular part of a matrix.
L = tril(M) L = tril(M,k)
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.
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
Extraction of the upper triangular part of a matrix.
U = triu(M) U = triu(M,k)
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.
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
Set union.
c = union(a, b) (c, ia, ib) = union(a, b)
union(a,b) gives the union of sets a and b, i.e. it gives the set of members of sets a or b or both. Sets are any type of numerical, character or logical arrays, or lists or cell arrays of character strings. Multiple elements of input arguments are considered as single members; the result is always sorted and has unique elements.
The optional second and third output arguments are vectors of indices such that if (c,ia,ib)=union(a,b), then elements of c are the elements of a(ia) or b(ib); the intersection of a(ia) and b(ib) is empty.
a = {'a','bc','bbb','de'}; b = {'z','bc','aa','bbb'}; (c, ia, ib) = union(a, b) c = {'a','aa','bbb','bc','de','z'} ia = 1 3 2 4 ib = 3 1 a(ia) {'a','bbb','bc','de'} b(ib) {'aa','z'}
Set exclusive or can also be computed as the union of a and b minus the intersection of a and b:
setdiff(union(a, b), intersect(a, b)) {'a','aa','de','z'}
unique, intersect, setdiff, setxor, ismember
Keep unique elements.
v2 = unique(v1) list2 = unique(list1) (b, ia, ib) = unique(a)
With an array argument, unique(v1) sorts its elements and removes duplicate elements. Unless v1 is a row vector, v1 is considered as a column vector.
With an argument which is a list of strings, unique(list) sorts its elements and removes duplicate elements.
The optional second output argument is set to a vector of indices such that if (b,ia)=unique(a), then b is a(ia).
The optional third output argument is set to a vector of indices such that if (b,ia,ib)=unique(a), then a is b(ib).
(b,ia,ib) = unique([4,7,3,8,7,1,3]) b = 1 3 4 7 8 ia = 6 3 1 2 4 ib = 3 4 2 5 4 1 2 unique({'def', 'ab', 'def', 'abc'}) {'ab', 'abc', 'def'}
sort, union, intersect, setdiff, setxor, ismember
Null array.
A = zeros(n) A = zeros(n1,n2,...) A = zeros([n1,n2,...]) A = zeros(..., type)
zeros builds an array whose elements are 0. The size of the array is specified by one integer for a square matrix, or several integers (either as separate arguments or in a vector) for an array of any size.
An additional input argument can be used to specify the type of the result. It must be the string 'double', 'single', 'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', or 'uint64' (64-bit arrays are not supported on all platforms).
zeros([2,3]) 0 0 0 0 0 0 zeros(2) 0 0 0 0 zeros(1, 5, 'uint16') 1x5 uint16 array 0 0 0 0 0