How can I find elements in sequence in an array?

조회 수: 25 (최근 30일)
ishita agrawal
ishita agrawal 2017년 5월 23일
댓글: ishita agrawal 2017년 5월 24일
I have an array of numbers. I want to find set of numbers for which difference of two consecutive numbers is 1. For example, I have a sequence [2,3,6,8,9,10,12,14,16,17]. How can I extract position of (2,3), (8,9,10), (16,17)? I also want to save the output in a new array.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2017년 5월 23일
v=[2,3,6,8,9,10,12,14,16,17]
id=[10 diff(v) 10]==1
ii1=strfind(id,[0 1])
ii2=strfind(id,[ 1 0])
  댓글 수: 3
Jan
Jan 2017년 5월 23일
To avoid a collision with the elements on the margins:
id = [false, diff(v)==1, false];
ishita agrawal
ishita agrawal 2017년 5월 24일
@Jan Simon Thank you for the suggestion.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by