How can I detect number that does not follow an increasing order?

I have a vector like this:
  • 1 2 3 4 100 5 6 7
or it could also be like this:
  • 153 158 161 163 172 123 179 181
if I look at the vector, I know I should delete 100 in 1st one and 123 in 2nd one. After that, either vector is in an increasing order.
I tried to use code diff(vector) to solve the problem in Matlab.
For 1st vector, the difference: 1 1 1 96 -95 1 1. The negative appears at 5th and I should delete No.5 in original sample.
For 2nd vector, the difference: 5 3 2 9 -49 56 2. The negative appears at 5th but I should delete No.6 in original sample.
The code diff cannot handle both cases at the same time. I am wondering if there is a more effective way.
Thank you very much!

5 Comments

For both vectors, besides keeping an increasing order, I would like to keep as many numbers as possible and delete as few numbers as possible.
The right result for the 1st vector should be: 1 2 3 5 6 7 and 2nd vector should be 153 158 161 163 172 179 181.
The solution to that problem is not unique. You might need to better define your conditions, I'm afraid.
Thanks, Jose-Luis. I agree with you, but I was reluctant to put more conditions. I think I have to do as you said...
perhaps some peak detection. performing the convolution of the pattern will lineup max of the convolution with the point that you want to take out. (works with b as well) [a;0 conv(a,[-.5 1 -.5],'valid') 0]
1.0000 2.0000 3.0000 4.0000 100.0000 5.0000 6.0000 7.0000
0 0 0 47.5000 95.5000 48.0000 0 0
and for b
153.0000 158.0000 161.0000 163.0000 172.0000 123.0000 179.0000 181.0000
0 1.0000 0.5000 3.5000 29.0000 52.5000 27.0000 0
might not work for all cases, more sophisticated methods for finding local maximum and local minimum may be what you are looking for.

Sign in to comment.

 Accepted Answer

s=[153 158 161 163 172 123 179 181 4 5 190]
while any(diff(s)<0)
ii=[1==0 diff(s)<0]
s(ii)=[];
end

4 Comments

Hi Azzi,
It works perfectly for the 2nd vector, but it returns 1 2 3 4 100 for the 1st case, which is not what I am looking for.
Do you have any idea that can work for both cases?
thank you very much!
Your problem is not clear, because from your question, the increasing order is
1 2 3 4 100
Thanks for pointing it out. I update my question.
this is not sufficient as criterion

Sign in to comment.

More Answers (0)

Categories

Asked:

on 24 Jun 2014

Edited:

on 24 Jun 2014

Community Treasure Hunt

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

Start Hunting!