Of course you can do this with accumarray
. x(end)
is the last observation in an array. Note that bin
needs to be sorted for this to work, so if it isn't, run
[bin,sortIdx]=sort(bin);x = x(sortIdx);
first.
lastobs = accumarray(bin(:),x(:),[],@(x)x(end)); %# bin, x, should be n-by-1