matrix_indexing - Digest Article Matrix Indexing in MATLAB...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Digest Article: Matrix Indexing in MATLAB 2002 Issues September July May March January 2001 Issues November September June March 2000 Issues December September June March Archived Articles 1999-1998 Subscribe Now Matrix Indexing in MATLAB ® by Steve Eddins and Loren Shure Send email to Steve Eddins and Loren Shure Indexing into a matrix is a means of selecting a subset of elements from the matrix. MATLAB has several indexing styles that are not only powerful and flexible, but also readable and expressive. Indexing is a key to MATLAB's effectiveness at capturing matrix-oriented ideas in understandable computer programs. Indexing is also closely related to another term MATLAB users often hear: vectorization. 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. To learn more about other similar methods, see the resources listed at the end of this article. Indexing Vectors Let's start with the simple case of a vector and a single subscript. The vector is v = [16 5 9 4 2 11 7 14]; The subscript can be a single value. v(3) % Extract the third element ans = 9 Or the subscript can itself be another vector. v([1 5 6]) % Extract the first, fifth, and sixth elements ans = 16 2 11 (1 of 8) [10/16/2002 1:01:06 AM]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Digest Article: Matrix Indexing in MATLAB MATLAB's colon notation provides an easy way to extract a range of elements from v . v(3:7) % Extract the third through the seventh elements ans = 9 4 2 11 7 Swap the two halves of v to make a new vector. v2 = v([5:8 1:4]) % Extract and swap the halves of v v2 = 2 11 7 14 16 5 9 4 The special end operator is an easy short-hand way to refer to the last element of v . v(end) % Extract the last element ans = 14 The end operator can be used in a range. v(5:end) % Extract the fifth through the last elements ans = 2 11 7 14 You can even do arithmetic using end. v(2:end-1) % Extract the second through the next-to-last elements ans = 5 9 4 2 11 7 Combine 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)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/14/2011 for the course MATH 571 taught by Professor Staff during the Winter '08 term at University of Michigan.

Page1 / 8

matrix_indexing - Digest Article Matrix Indexing in MATLAB...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online