필터 지우기
필터 지우기

Removing a bunch of elements from a 1-D vector?

조회 수: 8 (최근 30일)
Dheeraj Kapur
Dheeraj Kapur 2016년 1월 19일
편집: Stephen23 2016년 1월 22일
I have a large 1-D vector and want to delete ten consecutive elements after the first or second or third one (depending on the signal undergoing processing). For example if my starting vector is x = [1,2,3,4,5,6,7,8,9,10,11........10,000], I want to create another vector which looks like: y = [1,(delete ten elements),12, (delete ten elements),23,(delete ten elements),34,....]. What is the best way to do such an operation?

채택된 답변

Stephen23
Stephen23 2016년 1월 22일
편집: Stephen23 2016년 1월 22일
Use basic matrix indexing to select the elements that you want to keep:
>> X = randi(9,1,23)
X =
3 5 4 4 5 7 8 9 2 4 3 3 2 6 7 5 9 1 8 2 9 4 1
>> start = 3;
>> step = 5;
>> X(start:step:end)
ans =
4 9 2 1 1
Or for the original example:
>> Y = 1:10000;
>> start = 3;
>> step = 11;
>> Y(start:step:end)
ans =
Columns 1 through 14:
3 14 25 36 47 58 69 80 91 102 113 124 135 146
etc
Note that using indexing actually selects the values from the input vector.
  댓글 수: 1
Dheeraj Kapur
Dheeraj Kapur 2016년 1월 22일
This is precisely what I was looking to do. Thank you! Seems quite straight-forward when I look at it now.

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

추가 답변 (1개)

Stalin Samuel
Stalin Samuel 2016년 1월 19일
x = [1:1000];
gap = 10;%no of elements to be deleted
y = [min(x):gap+1:max(x)]
  댓글 수: 2
Dheeraj Kapur
Dheeraj Kapur 2016년 1월 19일
This works fine if I need to start with the first element of parent vector. Is there a way to do the same operation, say from the third element? As in, new vector looks something like y = [1,2,3,(delete 10 elements),14,(delete 10 elements), 25,....]. I was looking to get this done in the same way that we use to remove one element at a time. For eg x(3:5:end)=[] removes just one element. Can we use something along these lines to remove 10 elements at one go?
Stephen23
Stephen23 2016년 1월 22일
편집: Stephen23 2016년 1월 22일
@Dheeraj Kapur: this does not actually answer your question, because it does not select elements of the input vector as your original question requests, but instead creates an entirely new vector using the colon operator. This method will fail completely for any vector that is not a sequential sequence of integers with a step of one.
If you want a solution that actually selects elements of the input vector, regardless of their values, then see my answer. My method actually answers your question.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by