
Including these techniques and related functions in your MATLAB programming repertoire expands your ability to create masterful, readable, and vectorized code. We hope that the MATLAB indexing variants illustrated in this article give you a feel for ways you can express algorithms compactly and efficiently. You do it like this: nan_locations = find(isnan(A)) In this example, the computation is two-dimensional filtering using filter2. For example, suppose you want to temporarily replace NaN values with zeros, perform some computation, and then put the NaN values back in their original locations. Which form you use is mostly a matter of style and your sense of the readability of your code, but it also depends on whether or not you need the actual index values for something else in the computation. The expression A(A > 5) is equivalent to A(find(A > 5)). Logical indexing is closely related to the find function. Or you could replace all the spaces in a string matrix str with underscores. To replace all NaN elements of the matrix B with zero, use B(isnan(B)) = 0 If A is a multidimensional array, then max (A) operates along the first dimension of A whose size is greater than 1, treating the elements as vectors. If A is a matrix, then max (A) is a row vector containing the maximum value of each column of A. For example, you could replace all the NaN elements in an array with another value by using a combination of isnan, logical indexing, and scalar expansion. M max (A) returns the maximum elements of an array. Many MATLAB functions that start with is return logical arrays and are very useful for logical indexing. For example, A(A > 12) extracts all the elements of A that are greater than 12. The output is always in the form of a column vector. MATLAB extracts the matrix elements corresponding to the nonzero values of the logical array. In logical indexing, you use a single, logical array for the matrix subscript. This form of indexed assignment is called scalar expansion.Īnother indexing variation, logical indexing, has proven to be both useful and expressive. Why not just use max(mat,1) (gives maximum of each column) and max(mat,2) (gives maximum of each row) In any case, in the third line of your second function, y should be z. You can always, however, use a scalar on the right side: v() = 30 % Replace second and third elements by 30 Usually the number of elements on the right must be the same as the number of elements referred to by the indexing expression on the left. V(end:-1:1) % Reverse the order of elementsīy using an indexing expression on the left side of the equal sign, you can replace certain elements of the vector: v() = % Replace some elements of v You can even do arithmetic using end: v(2:end-1) % Extract the second through the next-to-last elementsĬombine the colon operator and end to achieve a variety of effects, such as extracting every k-th element or flipping the entire vector: v(1:2:end) % Extract all the odd elements The end operator can be used in a range: v(5:end) % Extract the fifth through the last elements The special end operator is an easy shorthand way to refer to the last element of v: v(end) % Extract the last element Swap the two halves of v to make a new vector: v2 = v() % Extract and swap the halves of v The colon notation in MATLAB provides an easy way to extract a range of elements from v: v(3:7) % Extract the third through the seventh elements Or the subscript can itself be another vector: v() % Extract the first, fifth, and sixth elements The subscript can be a single value: v(3) % Extract the third element Max, maxs, and mmax apply to dictionaries and tables.Let's start with the simple case of a vector and a single subscript. Range: bcdefghijmnpstuvxz Implicit iteration ¶ 0W -3 -2 -2 1 1 / remaining nulls replaced by preceding max Q)3 mmax 0N -3 -2 0N 1 0 / initial null returns negative infinity The first x items of the result are the maximums of the items so far, and thereafter the result is the moving maximum. Returns the x-item moving maximums of y, with nulls after the first replaced by the preceding maximum. domain: b g x h i j e f c s p m d z n u v t Q)maxs 0N 5 0N 1 3 / initial nulls return negative infinity

Nulls are ignored, except that initial nulls are returned as negative infinity. Where x is a non-symbol sortable list, returns the running maximums of its prefixes. domain: b g x h i j e f c s p m d z n u v t column of the image to find the maximum value of that column: find the maximum in the first dimension WesternBlotmaxColumn max(WesternBlot2,1) The. Q)select max price by sym from t / use in a select statement Q)max 0N 0N / negative infinity if all null Nulls are ignored, except that if the items of x are all nulls, the result is negative infinity.

Where x is a non-symbol sortable list, returns the maximum of its items.
