Detect consecutive positive numbers in an array

5 visualizaciones (últimos 30 días)
Amirhossein Moosavi
Amirhossein Moosavi el 24 de Sept. de 2020
Comentada: Amirhossein Moosavi el 24 de Sept. de 2020
Hello,
Let us assum an Array A as follows:
A = [4 9 6 0 0 3 4 0 3]
I want to detect the location (begining and ending) of consecutive positive values and store in a Matrix like B:
B=[1 3
6 7
9 9]
For example, in Array A, the first block of consequtive positive values starts from the first element and ends at the third elements.
Would you please help me?
Best, Amir

Respuesta aceptada

David Hill
David Hill el 24 de Sept. de 2020
Editada: David Hill el 24 de Sept. de 2020
A(A<0)=0;%if negative numbers possible
A(A>9)=9;%if >single digit possible
a=num2str(A);
a=a(a~=' ');
B=[regexp(a,'[1-9]*')',regexp(a,'[1-9]*','end')'];
  3 comentarios
David Hill
David Hill el 24 de Sept. de 2020
Works for me.
Amirhossein Moosavi
Amirhossein Moosavi el 24 de Sept. de 2020
It works now, thanks.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by