end for Classes
In a standard MATLAB® indexing expression,
end returns the index value of the last element in the dimension in which
end appears. For example, in
end method returns the index of the last element in the second
A. You can overload
end in classes for
Syntax and Default Behavior
This is the syntax MATLAB uses to call the
ind = end(A,k,n)
Ais the object being indexed into.
kis the dimension in the indexing expression where
nis the total number of indices in the expression.
indis the index value to use in the expression.
You cannot call the
end method directly using this syntax.
MATLAB automatically calls the method when it encounters end in an indexing
A is a 2-by-3 array of doubles. When MATLAB encounters the expression
A(end,1), it calls the
end method with these arguments.
Ais the object.
k = 1because
endappears in the first dimension of the indexing expression.
n = 2because the expression has two indices.
end method returns
2, which is the index of
the last element in the first dimension of
Any overload of the
end method must have the calling syntax
ind = end(A,k,n). For example, the modular indexing class
matlab.mixin.indexing.RedefinesParen has a built-in overload of
function ind = end(obj,k,n) sz = size(obj); if k < n ind = sz(k); else ind = prod(sz(k:end)); end end
if-elsestatement calculates the return value based on where the
endappears in the indexing expression and whether the indexing expression has values for all of the dimensions of the object array. For example, when
Bis a 2-by-3-by-2 object array of a type that inherits from
k < n: When
endis not the last value in the indexing expression, the overload returns the last value in that dimension. For
endreturns the size of the second dimension,
k = n: When
endis the last element in the indexing expression, the overload handles two cases:
If the indexing expression references all the indices, then
prod(sz(k:end))gives the same result as
sz(k). For example, in
If the indexing expression does not reference all the indices, then
prod(sz(k:end))returns the product of the size of dimension
kand the sizes of all unreferenced dimensions. For example, in
endreturns the product of the sizes of the second and third dimensions,
size as an abstract method for
the class author to implement, so the two methods are dependent on one another for the
final behavior. See the Customize Parentheses Indexing example for a class
that implements a
size method that provides the expected
end behavior with an array.