Skip to content

end

builtin · libnexusinterp/parse-tree/pt-eval-proprietary.cc

: end

Refer to the last element along a dimension, or close a program block.

Within an indexing expression end evaluates 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, end terminates a for, parfor, if, do, while, function, switch, try, or unwind_protect block.

Example:

x = [ 1 2 3; 4 5 6 ];
x(1,end)
 ⇒  3
x(end,1)
 ⇒  4
x(end,end)
 ⇒  6

Programming notes:

  1. The magic end index is recognized by the language’s own indexing syntax only; it has no effect inside manual subsref, subsasgn, or substruct calls.
  2. A user-defined class may give end a meaning of its own by supplying an end method. 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
    endfunction
    

    For more information see ‘Object Oriented Programming’.