% useful numeric functions % sum % mean % max % min % std % var % median % prctile % sort % sortrows a = randn(5,3); % 5 x 3 matrix of random zero-mean unit-variance normals sum(a) % sum along first dimension (row) sum(a,2) % sum along second dimension (column) mean(a) % mean along rows mean(a,2) % mean along columns min(a) % min along rows min(a,[],2) % min along columns -- why blank second argument? % becaues min with two arguments returns the minimum % between first and second argument % let's capture outputs of min mina = min(a,[],2) % the minimum values across columns [mina,mina_loc] = min(a,[],2) % the index of each column at which the min % occurs is also captured in a second % argument % max, like min, also has up to to output arguments (not shown below) max(a) % max along rows max(a,[],2) % max along columns (two-argument case similar to min) std(a) % standard deviation of values along rows var(a) % variance is just square of standard deviation % follows same function input/output conventions var(a,[],2) % variance deviation of values along columns % why blank second arugment? % second arugment used to tell matlab what kind of estimate % to make for the standard deviation. the default case is % to divide by n-1 in order to get an unbiased estimate % (default is best) median(a) % median along rows median(a,2) % median along columns prctile(a,10) % 10th percentile along rows prctile(a,10,2) % 10th percentile along columns prctile(a(:),10) % 10th percentile of all values in a % note m(:) for any matrix m will always produce % a row vector with the same number of elements as m % (it linearizes m) sort(a) % this produces a matrix, where each column has the same % values as each column in a, but in which the values % in those columns are sorted into increasing order % (we are sorting ALONG first dimension - row) sort(a,'descend') % same as before only descending sort(a,2) % this outputs a matrix in which each row has the same values % as a , except they are in increasing order % (we are sorting ALONG second dimension - column) sort(a,2,'descend') % sort along columns, descending % as with min and max, we can capture a second "index" arugment % here, asortedi will contain the column indexes sorted in descending order % for each row [asorted,asortedi] = sort(a,2,'descend') sortrows(a) % this is a matrix in which entire rows are preserved in order, % but in which the rows have been re-shuffled, so that the % first element is increasing down the matrix sortrows(a,-1) % same as above, except descending sortrows(a,[-1 2]) % break ties with first element according to increasing % second (column) element [arows,arowsi] = sortrows(a,2) % just increasing along second (column) element % arowsi contains the row indices of each % row, in order of increasing second column % value