What you're basically doing for each element in u
is finding the index of the largest element in t
. Try the following one-liner:
sum(bsxfun(@lt, repmat(t(:)', numel(u), 1), u(:)), 2)
What this does is:
- Generate a matrix using
repmat
, where each row equalst
. - Check each row using
bsxfun
for elements less than the corresponding element inu
. - Accumulate all the 1s in each row, essentially giving you the indices of the last smaller elements.
By the way, there is no need to put brackets ([]
) in t = [0:s:T]
. The colon operator (:
) already outputs a vector.