I would like to find the first element of a series of equal elements in a vector. For example I have
V = [1 2 2 2 3 3 3 4 4 2 2 3 3 4 4]
my result should be
R = [1 2 3 4 2 3 4]
Unfortunately I can't use unique because it would give me
R = [1 2 3 4];
Thanks
This works for this particular case:
R=V(find(diff([-Inf V])));
Same as
R=V(abs(diff([-Inf V]))>0);
(and the last one, as noted by Praetorian, can be expressed much more tidily as
R=V(diff([-Inf V])~=0);
saving you use of abs
Finally, if you want to be very logical,
R=V(~~diff([-Inf V]));
also works
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With