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

1 view (last 30 days)
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,

Accepted Answer

the cyclist
the cyclist on 26 Apr 2018
Edited: the cyclist on 26 Apr 2018
Use the unique function. Specifically, you can do
[~,y] = unique(x,'last')
  1 Comment
Frank B.
Frank B. on 26 Apr 2018
Thanks! Works perfectly. Didn't know about the 'last' option for the unique function.
Cheers,

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!