Reputation: 5959
Say you have an array, data, of unknown length. Is there a shorter method to get elements form a starting index to the end than
subdata = data(2:length(data))
Upvotes: 5
Views: 6248
Reputation: 725
You can use end
notation to indicate the last element. data(2:end)
returns a vector containing elements in the vector data
from element 2 to the last element. Or if data
is a character array, it returns the second character all the way to the last character. And data(end)
returns the last element.
This can be done with matrices too, i.e. data(2:end,5:end)
. Additionally you can use it as an operand, i.e. data(2:end-1)
, data(2:end/2)
.
In this context, end
serves a different purpose from its use at the end of functions/loops/switches.
Upvotes: 13