Indexing into a matrix is a means of selecting a subset of elements from the matrix.
Indexing is a key to the effectiveness of MATLAB at capturing matrix-oriented ideas in understandable computer programs. Vectorization means using MATLAB language constructs to eliminate program loops, usually resulting in programs that run faster and are more readable.
Of the many possible vectorization techniques, many rely on MATLAB indexing methods, five of which are described in this article.
The special end operator is an easy shorthand way to refer to the last element of v:. Combine the colon operator and end to achieve a variety of effects, such as extracting every k-th element or flipping the entire vector:.
Max return index matlab tutorial pdf
By using an indexing expression on the left side of the equal sign, you can replace certain elements of the vector:. 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. You can always, however, use a scalar on the right side:. Most often, indexing in matrices is done using two subscripts—one for the rows and one for the columns.
The simplest form just picks out a single element:. A single : in a subscript position is shorthand notation for 1:end and is often used to select entire rows or columns:. There is often confusion over how to select scattered elements from a matrix.
For example, suppose you want to extract the 2,1 , 3,2 , and 4,4 elements from A. The expression A [2 3 4], [1 2 4] won't do what you want. This diagram illustrates how two-subscript indexing works:. Extracting scattered elements from a matrix requires a different style of indexing, and that brings us to our next topic. When you index into the matrix A using only one subscript, MATLAB treats A as if its elements were strung out in a long column vector, by going down the columns consecutively, as in:.
The expression A 14 simply extracts the 14th element of the implicit column vector. Indexing into a matrix with a single subscript in this way is often called linear indexing.
Consider again the problem of extracting just the 2,1 , 3,2 , and 4,4 elements of A. You can use linear indexing to extract those elements:. That's easy to see for this example, but how do you compute linear indices in general? MATLAB provides a function called sub2ind that converts from row and column subscripts to linear indices.
You can use it to extract the desired elements this way:. But what if k is a function of the row number? That is, what if k is a vector of length m? Is there a quick and easy way to do this? Regular newsgroup contributor Peter Acklam posted this solution that uses sub2ind and linear indexing:.
Indexing Matrices with Two Subscripts
Another MATLAB user posted this question: I want to get the maximum of each row, which isn't really a problem, but afterwards I want to set all the other elements to zero. For example, this matrix:. Another indexing variation, logical indexing , has proven to be both useful and expressive.
In logical indexing, you use a single, logical array for the matrix subscript. The output is always in the form of a column vector.
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. To replace all NaN elements of the matrix B with zero, use.
Matrix Indexing in MATLAB
Logical indexing is closely related to the find function. 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.
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. In this example, the computation is two-dimensional filtering using filter2. You do it like this:.
We hope that the MATLAB indexing variants illustrated in this article give you a feel for ways you can express algorithms compactly and efficiently. Including these techniques and related functions in your MATLAB programming repertoire expands your ability to create masterful, readable, and vectorized code.
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
Select the China site in Chinese or English for best site performance. Other MathWorks country sites are not optimized for visits from your location. Toggle Main Navigation.
Technical Articles and Newsletters. Search MathWorks. Contact sales.
Using Sum, Min, Max and Find in Matlab
Indexing Vectors. Let's start with the simple case of a vector and a single subscript.
Select a Web Site
Indexing Matrices with Two Subscripts. Now consider indexing into a matrix. Linear Indexing. What does this expression A 14 do?
When you index into the matrix A using only one subscript, MATLAB treats A as if its elements were strung out in a long column vector, by going down the columns consecutively, as in: 16 5 Here are the elements of the matrix A along with their linear indices:.
The linear index of each element is shown in the upper left. From the diagram you can see that A 14 is the same as A 2,4. Advanced Examples Using Linear Indexing.
Example 1: Shifting the Rows of a Matrix. For example, this matrix: 1 2 3 4 5 5 6 5 7 9 8 3 should become: 0 0 0 4 0 0 6 0 0 9 0 0 Another regular newsgroup contributor, Brett Shoelson, provided this compact solution. Logical Indexing. Learn More. Select a Web Site Choose a web site to get translated content where available and see local events and offers. Select web site.