MATLAB: Condition which reduces the size of the vector

conditional relational operator.size of vector

Hi,
I have a query.
Generally relational operators are used to check whether some condition is true. i.e. it returns a logical vector. Is it possible that the new vector has the same values as the original vector (based on some conditions), but it must change the size of the vector neglecting the values which do not satisfy the condition. For eg, Suppose there is a vector with 10 elements [say (1,3,2,1,4,6,7,8,5,2)]and i put a condition that the new vector is formed such that it has elements which are only less than a value(say 5). Thus the new vector must have only 6 elements(neglecting the terms higher than 5) How is that possible?

Best Answer

v = [1,3,2,1,4,6,7,8,5,2];
vnew = v(v<5) % Returns: vnew = [1 3 2 1 4 2]