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. V() = 30 % Replace second and third elements by 30 You can always, however, use a scalar on the right side: 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(1:2:end) % Extract all the odd elements 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(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: V2 = v() % Extract and swap the halves of v Swap the two halves of v to make a new vector: V(3:7) % Extract the third through the seventh elements The colon notation in MATLAB provides an easy way to extract a range of elements from v: V() % Extract the first, fifth, and sixth elements Or the subscript can itself be another vector: Let's start with the simple case of a vector and a single subscript.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |