Delete some specific numbers from an array

So, the problem is like this. I have an array of random numbers. For example [10 9 5 5 7 3 5 8 3 5 6 3 7 10 ]. First, I have to delete some of the numbers to make any point's value can not between its adjecent numbers. For example 9 is between 10 and 5, so I will delete 9. I tried to use a for loop but that does not work.

2 comentarios

Walter Roberson
Walter Roberson el 2 de Oct. de 2019
Hint: sign(diff(TheVector)) will be the same for two entries in a row if the middle value is between the two other values (or if the three values are all equal)
Andrei Bobrov
Andrei Bobrov el 3 de Oct. de 2019
We delete 7, 7 is between 3 ans 10?

Iniciar sesión para comentar.

Respuestas (2)

Andrei Bobrov
Andrei Bobrov el 2 de Oct. de 2019
Editada: Andrei Bobrov el 3 de Oct. de 2019
A = [10 9 5 5 7 3 5 8 3 5 6 3 7 10 ];
B = sign(diff(A(hankel(1:3,3:numel(A)))',1,2));
out = A( [true; prod(B,2) ~= 1 ; true] );

2 comentarios

Rongyu Chu
Rongyu Chu el 3 de Oct. de 2019
My final purpose is not just delete 9 among 10 and 5. This is just an example. I will have a dat file which contians over 10,000 numbers and I need it to process automatically. Thank you for your help.
Andrei Bobrov
Andrei Bobrov el 3 de Oct. de 2019
I'm fix.

Iniciar sesión para comentar.

Rongyu Chu
Rongyu Chu el 3 de Oct. de 2019

0 votos

So, here is the cide I wrote.
N = load('exp.dat');
len = length(N);
for i = 2:(len-3)
if N(i+1)>N(i)>N(i-1)
N(i) = [];
len = len - 1;
%i = i - 1;
elseif N(i+1)<N(i)<N(i-1)
N(i) = [];
len = len - 1;
%i = i - 1;
end
end
I tired to use if sentence to compare the value and delete those numers whose value is in the middle of its adjcent numbers. However, this code does not work well. I am wondering why.

3 comentarios

Walter Roberson
Walter Roberson el 3 de Oct. de 2019
A<B<C does not mean that B must be in the range A to C. Instead it is ((A<B)<C) where the A<B returns 0 for false and 1 for true, and that 0 or 1 is compared to C.
Rongyu Chu
Rongyu Chu el 3 de Oct. de 2019
thank you. I realized that problem. But now I found a new problem. After I delete some numbers from an array, the arrat size gonna change. So it's not always len-1. But, how should I update that information ?
Walter Roberson
Walter Roberson el 3 de Oct. de 2019
Index from highest to lowest so that when you delete, you do not need to examine the entries that "fell down".

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 2 de Oct. de 2019

Editada:

el 3 de Oct. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by