This can be done using a for loop as shown below:
main.m129 chars9 lines
In the above code we are looping over the vector v
using a for
loop. We start by checking if the current value in the vector matches the next value. If it matches, then we can perform some operation, otherwise we set the current value to 0.
This will replace all non-consecutive values with 0. In case you only want to remove the non-consecutive values, and keep the consecutive ones, you can replace the v(i) = 0
with v(i) = []
, which will remove the element at that index.
Tags: vector
, for-loop
, conditional
gistlibby LogSnag