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))
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With