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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by