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
Let's start with the simple case of a vector and a single subscript.