How to remove 'middle' elements from a vector?

조회 수: 16 (최근 30일)
Maksym Sharma
Maksym Sharma 2019년 7월 29일
댓글: Maksym Sharma 2019년 7월 29일
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!

채택된 답변

per isakson
per isakson 2019년 7월 29일
편집: per isakson 2019년 7월 29일
Try
A( A>=2 & A<=7 ) = [];
  댓글 수: 1
Maksym Sharma
Maksym Sharma 2019년 7월 29일
hahahaha, exactly what i was missing out on! thanks alot !!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by