Chapter 4 Array indices and subscripts
Chapter 4 Array indices and subscripts To be written. 9
Chapter 5 Creating basic vectors, matrices and arrays 5.1 Creating a constant array 5.1.1 When the class is determined by the scalar to replicate To create an array whose size is siz =[m n p q.. and where each element has the value valuse x repmat(val, siz) Following are three other ways to achieve the same, all based on what repmat uses internally. Note hat for these to work, the array X should not already exist x(prod(siz))= val; array of right class and num. of elements x reshape(X, siz)is reshape to specified size x(:)=x(end) 各fi11va1′ into x( redundant if'va1′ Is zero) If the size is given as a cell vector siz =(m n p q ), there is no need to reshape X(si2{:})=va1; g array of right class and siz x(: )=x(end) 各Ei11va1′ into X( redundant if'va1′ 15 zero) Ifm, n, p, q,.. are scalar variables, one may use X(m,n,p,g)=val s array of right class and size 号fi11va1′ into x( redundant if'va1′ Is zero) The following way of creating a constant array is frequently used but this solution requires more memory since it creates an index array. Since an index array is used, it only works if val is a variable, whereas the other solutions above also work when val is a function e.g., if val is Inf or Na s this won't work unless nan is a variable x repmat(NaN, siz) 8 here NaN may be a function or a variable Avoid
Chapter 5 Creating basic vectors, matrices and arrays 5.1 Creating a constant array 5.1.1 When the class is determined by the scalar to replicate To create an array whose size is siz =[m n p q ...] and where each element has the value val, use X = repmat(val, siz); Following are three other ways to achieve the same, all based on what repmat uses internally. Note that for these to work, the array X should not already exist X(prod(siz)) = val; % array of right class and num. of elements X = reshape(X, siz); % reshape to specified size X(:) = X(end); % fill ‘val’ into X (redundant if ‘val’ is zero) If the size is given as a cell vector siz ={m n p q ...}, there is no need to reshape X(siz{:}) = val; % array of right class and size X(:) = X(end); % fill ‘val’ into ‘X’ (redundant if ‘val’ is zero) If m, n, p, q, . . . are scalar variables, one may use X(m,n,p,q) = val; % array of right class and size X(:) = X(end); % fill ‘val’ into X (redundant if ‘val’ is zero) The following way of creating a constant array is frequently used X = val(ones(siz)); but this solution requires more memory since it creates an index array. Since an index array is used, it only works if val is a variable, whereas the other solutions above also work when val is a function returning a scalar value, e.g., if val is Inf or NaN: X = NaN(ones(siz)); % this won’t work unless NaN is a variable X = repmat(NaN, siz); % here NaN may be a function or a variable Avoid using 10
CHAPTER 5. CREATING BASIC VECTORS, MATRICES AND ARRAYS x val ones(siz)i since it does unnecessary multiplications and only works for classes for which the multiplication operator is defined. 5.1.2 When the class is stored in a string variable To create an array of an arbitrary class cls, where cls is a character array (i.e, string) containing he class name, use any of the above which allows val to be a function call and let val be feval(cls, val) As a special case, to create an array of class cls with only zeros, you can use X (prod(siz))= feval(cls, 0)i Avoid user x feval(cls, zeros(siz) 8 might require a lot more memory since it first creates an array of class double which might require many times more memory than x if an array of class cls requires less memory pr element than a double array 5.2 Special vectors 5.2.1 Uniformly spaced elements To create a vector of uniformly spaced elements, use the linspace function or the:(colon) operator x linspace (lower, upper,n s row vector x linspace(lower, upper, n).' 各 column vector step s row vector lower step upper ).'i s column vector If the difference upper-lower is not a multiple of step, the last element of X, x(end), will be less than upper. So the condition A (end)<= upper is always satisfied
CHAPTER 5. CREATING BASIC VECTORS, MATRICES AND ARRAYS 11 X = val * ones(siz); since it does unnecessary multiplications and only works for classes for which the multiplication operator is defined. 5.1.2 When the class is stored in a string variable To create an array of an arbitrary class cls, where cls is a character array (i.e., string) containing the class name, use any of the above which allows val to be a function call and let val be feval(cls, val) As a special case, to create an array of class cls with only zeros, you can use X = repmat(feval(cls, 0), siz); % a nice one-liner or X(prod(siz)) = feval(cls, 0); X = reshape(X, siz); Avoid using X = feval(cls, zeros(siz)); % might require a lot more memory since it first creates an array of class double which might require many times more memory than X if an array of class cls requires less memory pr element than a double array. 5.2 Special vectors 5.2.1 Uniformly spaced elements To create a vector of uniformly spaced elements, use the linspace function or the : (colon) operator: X = linspace(lower, upper, n); % row vector X = linspace(lower, upper, n).’; % column vector X = lower : step : upper; % row vector X = ( lower : step : upper ).’; % column vector If the difference upper-lower is not a multiple of step, the last element of X, X(end), will be less than upper. So the condition A(end) <= upper is always satisfied
Chapter 6 Shifting 6.lⅤ ectors To shift and rotate the elements of a vector. use x([ end 1: end-1 ])i 号 shift ht/down 1 el x([ end-k+l: end 1: end-k 1) 8 shift right/down k eleme X([ 2: end 1])i 8 shift left/up 1 element X([k+1:end1:k]); 8 shift left/up k element Note that these only work if k is non-negative. If k is an arbitrary integer one may use something x( mod((1: end)-k-l, end)+1 g shift right/down k elements x( mod((l: end)+k-l, end)+1 号 shift1eft/upk where a negative k will shift in the opposite direction of a positive k 6.2 Matrices and arrays To shift and rotate the elements of an array x along dimension dim, first initialize a subscript cell array with idx repmat[':'), dims(X), 1)i initialize subscripts n= size(x, dim i length along dimension dim then manipulate the subscript cell array as appropriate by using one of idxidim=[n l: n-1 8 shift right/down/forwards 1 element idxtdim = n-k+l:n l: n-k]i 8 shift right/down/forwards k elements dx{dim}=[2:n1]; 8 shift left/up/backwards 1 element dxidim) =[ k+l:n 1: k I 8 shift left/up/backwards k elements finally create the new array Y =x(idx(:1)
Chapter 6 Shifting 6.1 Vectors To shift and rotate the elements of a vector, use X([ end 1:end-1 ]); % shift right/down 1 element X([ end-k+1:end 1:end-k ]); % shift right/down k elements X([ 2:end 1 ]); % shift left/up 1 element X([ k+1:end 1:k ]); % shift left/up k elements Note that these only work if k is non-negative. If k is an arbitrary integer one may use something like X( mod((1:end)-k-1, end)+1 ); % shift right/down k elements X( mod((1:end)+k-1, end)+1 ); % shift left/up k element where a negative k will shift in the opposite direction of a positive k. 6.2 Matrices and arrays To shift and rotate the elements of an array X along dimension dim, first initialize a subscript cell array with idx = repmat({’:’}, ndims(X), 1); % initialize subscripts n = size(X, dim); % length along dimension dim then manipulate the subscript cell array as appropriate by using one of idx{dim} = [ n 1:n-1 ]; % shift right/down/forwards 1 element idx{dim} = [ n-k+1:n 1:n-k ]; % shift right/down/forwards k elements idx{dim} = [ 2:n 1 ]; % shift left/up/backwards 1 element idx{dim} = [ k+1:n 1:k ]; % shift left/up/backwards k elements finally create the new array Y = X(idx{:}); 12
Chapter 7 Replicating elements and arrays 7.1 Creating a constant array See section 5.1 7.2 Replicating elements in vectors 7.2.1 Replicate each element a constant number of times Example Given create N copies of each element in A, So B=[444555 for instance B= A(ones(1, N),: B=B(:). If A is a column-vector B=A(:, ones(1, N)) B=B(:); Some people use B= A( ceil((1: N*length(A))/N ))i but this requires unnecessary arithmetic. The only advantage is that it works regardless of whether A is a row or column vector 7.2.2 Replicate each element a variable number of times See section 15.5.2 about run-length decoding
Chapter 7 Replicating elements and arrays 7.1 Creating a constant array See section 5.1. 7.2 Replicating elements in vectors 7.2.1 Replicate each element a constant number of times Example Given N = 3; A = [ 4 5 ] create N copies of each element in A, so B = [ 4 4 4 5 5 5 ] Use, for instance, B = A(ones(1,N),:); B = B(:).’; If A is a column-vector, use B = A(:,ones(1,N)).’; B = B(:); Some people use B = A( ceil( (1:N*length(A))/N ) ); but this requires unnecessary arithmetic. The only advantage is that it works regardless of whether A is a row or column vector. 7.2.2 Replicate each element a variable number of times See section 15.5.2 about run-length decoding. 13