Loop through a vector with changing legth
2 views (last 30 days)
Hello everyone. I want to loop through a vector, say z, and at each iteration remove some elements from it, using something like
z = z(abs(z-z(i)>=1))
My problem is, I don't know how to form the loop. I tried using
for member = z
but then again, I need to do calculations with each element so that doesn't work. Can anyone help me?
More Answers (1)
Jan on 22 Mar 2018
A loop over the elements of a vector cannot work, if you remove elements of the vector, except if you process the elements from the end to the start and remove only elements, which are after the current element.
z = rand(1, 10)
for k = 9:-1:1
if z(k) < 0.5
z(k+1) = ;
This cannot work, if you run it in the opposite direction from 1 to 9.
So what does "loop through a vector" exactly mean, if you remove elements? It cannot be an "element by element". What should happen exactly with the vector? You have to define this clearly, before it can be implemented.