CHAPTER 2. OPERATORS. FUNCTIONS AND SPECIAL CHARACTERS 2.2 Built-in functions True if all elements of a vector are nonzero True if any element of a vector is nonzero cumsum Cumulative sum of elements Diagonal matrices and diagonals of a matrix. diff Difference and approximate derivative Last index in an indexing expression Identity matrix find Find indices of nonzero elements empty True for empty True if arrays are numerically eq isfinite True for finite elements ising True for infinite elements islogical True for logical array . snan True for Not-a-Number isnumeric True for numeric arrays eng gth of vector logical Convert numeric values to logical dims Number of dimension numen Number of elements in a matrix ones Ones array. permute Permute array dimensions prod Product of element reshape Change size Size of matrix sort Sort in ascending order sum Sum of elements tril Extract lower triangular part. triu Extract upper triangular part zeros Z Some of these functions are shorthands for com binations of other built-in functions lik length(x) Is max(size(x)) ndis(x) length(size(x)) numen(x) prod(size(x)) Others are shorthands for frequently used tests, like isempty(x)Is isif (x) isfinite(x) abs(x)w= Inf Others are shorthands for frequently used functions which could have been written with low-level cumsum, cumprod, sort, tril, triu, etc
CHAPTER 2. OPERATORS, FUNCTIONS AND SPECIAL CHARACTERS 4 2.2 Built-in functions all True if all elements of a vector are nonzero. any True if any element of a vector is nonzero. cumsum Cumulative sum of elements. diag Diagonal matrices and diagonals of a matrix. diff Difference and approximate derivative. end Last index in an indexing expression. eye Identity matrix. find Find indices of nonzero elements. isempty True for empty matrix. isequal True if arrays are numerically equal. isfinite True for finite elements. isinf True for infinite elements. islogical True for logical array. isnan True for Not-a-Number. isnumeric True for numeric arrays. length Length of vector. logical Convert numeric values to logical. ndims Number of dimensions. numel Number of elements in a matrix. ones Ones array. permute Permute array dimensions. prod Product of elements. reshape Change size. size Size of matrix. sort Sort in ascending order. sum Sum of elements. tril Extract lower triangular part. triu Extract upper triangular part. zeros Zeros array. Some of these functions are shorthands for combinations of other built-in functions, lik length(x) is max(size(x)) ndims(x) is length(size(x)) numel(x) is prod(size(x)) Others are shorthands for frequently used tests, like isempty(x) is numel(x) == 0 isinf(x) is abs(x) == Inf isfinite(x) is abs(x) ~= Inf Others are shorthands for frequently used functions which could have been written with low-level code, like diag, eye, find, sum, cumsum, cumprod, sort, tril, triu, etc
CHAPTER 2. OPERATORS. FUNCTIONS AND SPECIAL CHARACTERS 2.3 M-file functions flipdi Flip matrix along specified dimension flipl Flip matrix in left/right direction fliud Flip matrix in up/down direction ind2sub Multiple subscripts from linear index Inverse permute array dir kron Kronecker tensor produc nspace Linearly spaced vector. hagrid Generation of arrays for N-D functions and interpolation repmat Replicate and tile an array rote Rotate matrix 90 degrees. shittim hift di squeeze Remove singleton dimensions. sub2ind Linear index from multiple subscripts
CHAPTER 2. OPERATORS, FUNCTIONS AND SPECIAL CHARACTERS 5 2.3 M-file functions flipdim Flip matrix along specified dimension. fliplr Flip matrix in left/right direction. flipud Flip matrix in up/down direction. ind2sub Multiple subscripts from linear index. ipermute Inverse permute array dimensions. kron Kronecker tensor product. linspace Linearly spaced vector. ndgrid Generation of arrays for N-D functions and interpolation. repmat Replicate and tile an array. rot90 Rotate matrix 90 degrees. shiftdim Shift dimensions. squeeze Remove singleton dimensions. sub2ind Linear index from multiple subscripts
Chapter 3 Basic array properties 3.1 Size The size of an array is a row vector with the length along all dimensions. The size of the array x can be found with sx size(x)i 8 size of x(along all dimensions) The length of the size vector sx is the number of dimensions in x. That is, length(size(x)) is identical to dims()(see section 3.2. 1). No builtin array class in Matlab has less than two dimensions To change the size of an array without changing the number of elements, use reshape 3.1.1 Size along a specific dimension To get the length along a specific dimension dim, of the array x,use size(x, dim g size of x(along a specific dimension) This will return one for all singleton dimensions(see section 3.2.2), and, in particular, it will return one for all dim greater than dims(x) 3.1.2 Size along multiple dimensions Sometimes one needs to get the size along multiple dimensions. It would be nice if we could use dimension argument to be a scalar. We may of course use a for-loop solution like size(x, dims), where dims is a vector of dimension numbers, but alas, size only allows the siz zeros(size(dims))i s initialize size vector to return 1: numel(dims) i loop over the elements in dims siz(i)=size(x, dims(i))i get the size along dimension end 8 end loop A vectorized version of the above is siz ones(size(dims))i initialize size vector to return sx size(x)i 各 get size a⊥onga11 dimensions k dims < ndis(x)i i dimensions known not to be trailing singleton siz(k)= sx(dims(k))i g insert size along dimensions of interest
Chapter 3 Basic array properties 3.1 Size The size of an array is a row vector with the length along all dimensions. The size of the array x can be found with sx = size(x); % size of x (along all dimensions) The length of the size vector sx is the number of dimensions in x. That is, length(size(x)) is identical to ndims(x) (see section 3.2.1). No builtin array class in MATLAB has less than two dimensions. To change the size of an array without changing the number of elements, use reshape. 3.1.1 Size along a specific dimension To get the length along a specific dimension dim, of the array x, use size(x, dim) % size of x (along a specific dimension) This will return one for all singleton dimensions (see section 3.2.2), and, in particular, it will return one for all dim greater than ndims(x). 3.1.2 Size along multiple dimensions Sometimes one needs to get the size along multiple dimensions. It would be nice if we could use size(x, dims), where dims is a vector of dimension numbers, but alas, size only allows the dimension argument to be a scalar. We may of course use a for-loop solution like siz = zeros(size(dims)); % initialize size vector to return for i = 1 : numel(dims) % loop over the elements in dims siz(i) = size(x, dims(i)); % get the size along dimension end % end loop A vectorized version of the above is siz = ones(size(dims)); % initialize size vector to return sx = size(x); % get size along all dimensions k = dims <= ndims(x); % dimensions known not to be trailing singleton siz(k) = sx(dims(k)); % insert size along dimensions of interest 6
CHAPTER 3. BASIC ARRAY PROPERTIES which is the essential part of the function mttsize in the MTT Toolbox Code like the following is sometimes seen, unfortunately. It might be more intuitive than the above, but it is more fragile since it might use a lot of memory when dims contains a large value sx size(x)i 3x=m8x(1()-n1m(x s get size along all dimensions 8 number of dimensions to append s(1 of interest An unlikely scenario perhaps, but imagine what happens if x and dims both are scalars and that dims is a billion. The above code would require more than 8 GB of memory. The suggested solution further above requires a negligible amount of memory. There is no reason to write fragile code when it can easily be avoided 3.2 Dimensions 3.2.1 Number of dimensions The number of dimensions of an array is the number of the highest non-singleton dimension( see section 3.2.2)but never less than two since arrays in MATLaB always have at least two dimensions The function which returns the number of dimensions is ndims. so the num ber of dimensions of an array x Is dx dims (x) number of dimensions One may also say that ndims (x)is the largest value of dim, no less than two, for which size(x, dim is different from one Here are a few examples x ones(2, 1) 8 2-dimensional 8 2-dimensional x= ones(1, 0) 8 2-dimensional x ones(1, 2,3,0,0)9 5-dimensional x= ones(2,3,0,0,1)s 4-dimensional x= ones(3,0,0,1, 2)9 5-dimensional 3.2.2 Singleton dimensions A"singleton dimension"is a dimension along which the length is one. That is, if size(x, dim) is one, then dim is a singleton dimension. If, in addition, dim is larger than dims(x), then dim is called a trailing singleton dimension. Trailing singleton dimensions are ignored by size and ndis Singleton dimensions may be removed with squeeze. Removing singleton dimensions chan ges the size of an array, but it does not change the number of elements in an array Flipping an array along a singleton dimension is a null-operation, that is, it has no effect, it 3.3 Number of elements The number of elements in an array may be obtained with numel, e.g., numel (x)is the number of elements in x. The number of elements is simply the product of the length along all dimension that is, prod (size(x))
CHAPTER 3. BASIC ARRAY PROPERTIES 7 which is the essential part of the function mttsize in the MTT Toolbox. Code like the following is sometimes seen, unfortunately. It might be more intuitive than the above, but it is more fragile since it might use a lot of memory when dims contains a large value. sx = size(x); % get size along all dimensions n = max(dims(:)) - ndims(x); % number of dimensions to append sx = [ sx ones(1, n) ]; % pad size vector siz = sx(dims); % extract dimensions of interest An unlikely scenario perhaps, but imagine what happens if x and dims both are scalars and that dims is a billion. The above code would require more than 8 GB of memory. The suggested solution further above requires a negligible amount of memory. There is no reason to write fragile code when it can easily be avoided. 3.2 Dimensions 3.2.1 Number of dimensions The number of dimensions of an array is the number of the highest non-singleton dimension (see section 3.2.2) but never less than two since arrays in MATLAB always have at least two dimensions. The function which returns the number of dimensions is ndims, so the number of dimensions of an array x is dx = ndims(x); % number of dimensions One may also say that ndims(x) is the largest value of dim, no less than two, for which size(x,dim) is different from one. Here are a few examples x = ones(2,1) % 2-dimensional x = ones(2,1,1,1) % 2-dimensional x = ones(1,0) % 2-dimensional x = ones(1,2,3,0,0) % 5-dimensional x = ones(2,3,0,0,1) % 4-dimensional x = ones(3,0,0,1,2) % 5-dimensional 3.2.2 Singleton dimensions A “singleton dimension” is a dimension along which the length is one. That is, if size(x,dim) is one, then dim is a singleton dimension. If, in addition, dim is larger than ndims(x), then dim is called a “trailing singleton dimension”. Trailing singleton dimensions are ignored by size and ndims. Singleton dimensions may be removed with squeeze. Removing singleton dimensions changes the size of an array, but it does not change the number of elements in an array Flipping an array along a singleton dimension is a null-operation, that is, it has no effect, it changes nothing. 3.3 Number of elements The number of elements in an array may be obtained with numel, e.g., numel(x) is the number of elements in x. The number of elements is simply the product of the length along all dimensions, that is, prod(size(x))
CHAPTER 3. BASIC ARRAY PROPERTIES 3.3.1 Empty arrays If the length along at least one dimension is zero, then the array has zero elements, and hence it is empty. We could test the array x for emptiness with any (size(x)==0)or numel(x)==0, but there is a builtin function which explicitly tests for emptiness, isempty
CHAPTER 3. BASIC ARRAY PROPERTIES 8 3.3.1 Empty arrays If the length along at least one dimension is zero, then the array has zero elements, and hence it is empty. We could test the array x for emptiness with any(size(x) == 0) or numel(x) == 0, but there is a builtin function which explicitly tests for emptiness, isempty