Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect changes in vector

Tags:

vector

matlab

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

like image 326
gabboshow Avatar asked Aug 08 '26 04:08

gabboshow


1 Answers

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

like image 187
Buck Thorn Avatar answered Aug 11 '26 09:08

Buck Thorn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!