Effacer les filtres
Effacer les filtres

How can I find elements in sequence in an array?

15 vues (au cours des 30 derniers jours)
ishita agrawal
ishita agrawal le 23 Mai 2017
Commenté : ishita agrawal le 24 Mai 2017
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.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 23 Mai 2017
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 commentaires
Jan
Jan le 23 Mai 2017
To avoid a collision with the elements on the margins:
id = [false, diff(v)==1, false];
ishita agrawal
ishita agrawal le 24 Mai 2017
@Jan Simon Thank you for the suggestion.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by