Individual entries can be extracted from a matrix by simply specifying the indices inside round brackets. A = ,ones(1,3)] % concatenate, along columnsī = ones(1,3)] % concatenate, along rowsĬ = % add a number at the beginning or end of an array Care must be taken that the matrices are of the right size or Matlab will return an error. Matrices can be concatenated by enclosing them inside of square brackets and using either a space or semicolon to specify the dimension. G = min(A,1) % Find the min of each column (collapse dim 1) F1 = max(A, 2) % Larger of A and 2 elementwiseį = max(A,2) % Find the max of each row (collapse dim 2) The argument to the min and max functions indicates that you will specify a dimension. C = sum(A,1) % Sum out dimension 1, (rows)ĭ = sum(A,2) % Sum out dimension 2, (cols)Į = mean(A,1) % Take the average along dimension 1, (rows) You can use the sum() and mean() functions to sum up or take the average of entries along a certain dimension. n = size(A,2)Ī m-by-n matrix can be transposed into a n-by-m matrix by using the transpose operator '. We can also determine the size along a specific dimension with size(). The length() command gives the number of elements in the first non-singleton dimension, and is frequently used when the input is a row or column vector however, it can make code less readable as it fails to make the dimensionality of the input explicit. We refer to dimensions of size 1 as singleton dimensions. We can determine the size of a matrix by using the size() command = size(A)Īnd the number of elements by using the numel() command. The functions true() and false(), act just like ones() and zeros() but create logical arrays whose entries take only 1 byte each rather than 32. L = triu(ones(3,4)) % 3-by-4 matrix whose upper triangular part is all ones. K = tril(ones(3,4)) % 3-by-4 matrix whose lower triangular part is all ones. J = blkdiag(rand(2,2),ones(3,2)) % 5-by-4 block diagonal matrix I = logspace(0,2,6) % 1-by-6 matrix of log-spaced numbers from 10^0 to 10^2 = meshgrid(1:5) % 5-by-5 grids of numbers A = zeros(4,5) % 4-by-5 matrix of all zerosī = ones (2,3) % 2-by-3 matrix of all onesĬ = rand(3,3) % 3-by-3 matrix of uniform random numbers in ĭ = randn(2,5) % 2-by-5 matrix of standard normally distributed numbers D = Īlternatively, there are several functions that will generate matrices for us. A = 1:10 % start at 1, increment by 1, stop at 10ī = 1:2:10 % start at 1, increment by 2, stop at 10Ĭ = 10:-1:3 % start at 10, decrement by 1, stop at 3 We can often exploit patterns in the entries to create matrices more succinctly. We, (and Matlab) always refer to rows first and columns second. We say that this matrix is of size 4-by-3 indicating that it has 4 rows and 3 columns. Entries on each row are separated by a space or comma and rows are separated by semicolons, (or newlines). We begin by simply entering data directly. There are a number of ways to create a matrix in Matlab.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |