How to cut a vector in intervals?

9 vues (au cours des 30 derniers jours)
Dada
Dada le 17 Oct 2016
Commenté : Dada le 17 Oct 2016
I need to cut a vector in a determined intervals. The size of my vector is unknown because it depends on the previous input. Lest say my vector is [2:97] and I need it to be cut in intervals of two (I previously made sure the vector can be divided by three) and all put together in a cell. The result has to be {[2 3 4],[5 6 7],[8 9 10],...,[95 96 97]}. How can I do that?

Réponse acceptée

Guillaume
Guillaume le 17 Oct 2016
v = 2:97;
intervallength = 3;
assert(isrow(v), 'input not a row vector');
assert(mod(numel(v), intervallength) == 0, 'vector length not divisible by interval length')
splitv = mat2cell(v, 1, ones(1, numel(v)/intervallength) * intervallength);
  1 commentaire
Dada
Dada le 17 Oct 2016
That's exactly it! Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graph and Network Algorithms 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