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

 채택된 답변

David Hill
David Hill 2020년 9월 24일
편집: David Hill 2020년 9월 24일

1 개 추천

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

Thanks, but it is not generalizable. For example, it does not work for the below array:
A = [4 9 6 0 0 3 4 0 10 0 15 0 0 11 14 0 1]
Works for me.
It works now, thanks.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by