MATLAB: Find the latest indexes in a vector, without for loop.

findindex

Let's say I have the following vector:
x = [1 2 3 4 3 4 5 6 7 8 5 6 7 8 9 10];
Is there a way, WITHOUT a for loop, to find the indexes of the each of the latest value (ie: the last "5" is at x(11)) so the resulting vector is the following:
y = [1 2 5 6 11 12 13 14 15 16];
Now I can do that with a for loop, just curious to know if there's a more elegant and/or efficient way. I couldn't figure it out.
Cheers,

Best Answer

Use the unique function. Specifically, you can do
[~,y] = unique(x,'last')
Related Question