How can I delete elements in a vector depending on the difference between them?

Hi
I would like to delete certain elements in a vector so that I end up with a vector in which the difference between one element and the consecutive one is bigger than a given treshold?
Example:
a = [1 2 3 4 5 6 7 8 9];
treshold = 2;
And I want to end up with b = [1 4 7];
Many thanks in advance.

 채택된 답변

Here is a very straightforward method:
a = [1 2 3 4 5 6 7 8 9];
b = a;
threshold = 2;
idx = 1;
while idx < numel(b-1)
if b(idx+1) <= b(idx) + threshold
b(idx+1) = [];
else
idx = idx + 1;
end
end

댓글 수: 1

That works fine, many thanks! I found quite useful how you change the size of the array within the while loop, what you couldn't do in a for loop.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

질문:

2015년 11월 12일

댓글:

2015년 11월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by