MATLAB Answers

How to remove 'middle' elements from a vector?

32 views (last 30 days)
Hello everybody,
I am finding dificulty in making a one liner for removing middle values from my input vector 'A'. The column vector is a 35000 x 1 (double) with values inside it ranging anywhere from 0 to 250 (not in order! ; the vector looks like a "sine wave" when plotted). Now I would like to remove all values from 2 to 7 in that vector (so, A ~= Values ; where Values = 2:7) but preserving the order of all the other numbers in 'A' (so that its still a wave with same structure and amplitude, but just compressed a little bit due to removal of all 2s,3s,...,7s from it). I am using all values A == 1 (all 'ones') as a marker for colouring due to data structure of 'A' and thus I want to keep these values in the same position in my original vector 'A'. I tried a few things but its not what I want:
%% method 1
A = 'MyVector_Here' % can't post the vector here since it is patient data...
a1 = A(find(A>7));
a2 = A(find(A<2));
B = [a1 a2]; % B = union(a1,a2)?
%% Method 2
A = 'MyVector_Here'
Vals = [2:7]' % []' not necessary
B = setdiff(A, Vals);
These dont work due to rearrangement of my values. So the final vector 'B' is something like a ~33000 x 1 (double) compared to the initial ~35000 x 1 vector 'A'. Please let me know if there is an easy 'in-built' function to do this that exists OR is making some kind of a for loop to go through each element is the only wayout?
To recap - I would like to remove a series of values without disrupting ANYTHING ELSE AT ALL in the original vector ; just a "silent trimming" of a vector, preferably in 1 line of code.
Any help or suggestion with this problem is appreciated. Thanks in advance!


Sign in to comment.

Accepted Answer

per isakson
per isakson on 29 Jul 2019
Edited: per isakson on 29 Jul 2019
A( A>=2 & A<=7 ) = [];

  1 Comment

Maksym Sharma
Maksym Sharma on 29 Jul 2019
hahahaha, exactly what i was missing out on! thanks alot !!!

Sign in to comment.

More Answers (0)

Sign in to answer this question.