Effacer les filtres
Effacer les filtres

Find order of increasing integers

1 vue (au cours des 30 derniers jours)
Sebastian Daneli
Sebastian Daneli le 15 Juin 2021
Commenté : Sebastian Daneli le 15 Juin 2021
I have a vector
[3,4,8,9,10,13]
How can I sort this into
[3,4]; [8,9,10]; [13];
in an efficient way? I.e., split the parts of the vector where there is a linear increase of +1 from the previous entry.

Réponse acceptée

Stephen23
Stephen23 le 15 Juin 2021
Modifié(e) : Stephen23 le 15 Juin 2021
V = [3,4,8,9,10,13];
D = diff(find([true,diff(V)~=1,true]));
C = mat2cell(V,1,D)
C = 1×3 cell array
{[3 4]} {[8 9 10]} {[13]}
  1 commentaire
Sebastian Daneli
Sebastian Daneli le 15 Juin 2021
Thx, works great.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices 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