end
- : end ¶
Refer to the last element along a dimension, or close a program block.
Within an indexing expression
endevaluates to the highest valid subscript for the dimension in which it appears, which is a convenient way to reach the final element (or the final row, column, or page) of an array without first querying its size.Away from indexing,
endterminates afor,parfor,if,do,while,function,switch,try, orunwind_protectblock.Example:
x = [ 1 2 3; 4 5 6 ]; x(1,end) ⇒ 3 x(end,1) ⇒ 4 x(end,end) ⇒ 6
Programming notes:
- The magic
endindex is recognized by the language’s own indexing syntax only; it has no effect inside manualsubsref,subsasgn, orsubstructcalls. - A user-defined class may give
enda meaning of its own by supplying anendmethod. That method is handed the object, the position of the dimension being indexed, and the total number of index positions, and returns the last index to use:function last_index = end (obj, end_dim, ndim_obj) if (end_dim == ndim_obj) last_index = prod (size (obj)(end_dim:ndim_obj)); else last_index = size (obj, end_dim); endif endfunctionFor more information see ‘Object Oriented Programming’.
- The magic