Cut vector into many new vectors with defined lengths

I have a very long vector that I would like to cut up into new vectors of a specified length (400).

 Accepted Answer

The easiest way to do what you want would be to use the mat2cell command. For instance, if you wanted to divide it into segments of lengths of [100, 150, 50, 25, 75]:
V = randi(99, 1, 400);
Vs = mat2cell(V, 1, [100, 150, 50, 25, 75]);
Vs4 = Vs{4}; % Addressing Cell #4
Here, ‘Vs’ is a (1x5) cell, and ‘Vs4’ is a (1x25) double.

2 Comments

or depending on your implementation and how you want to use it, reshape may work out and instead of a 1xN vector you'll then make it 400xN/400 matrix where each column is your "new" vector
Remember that reshape goes down rows, so you might want to transpose:
Vmat = reshape(V, 400, []).';
Reshape will require that the vector be an exact multiple of the target size (400).

Sign in to comment.

More Answers (2)

If you have the signal processing toolkit, you may wish to use buffer(), which is like reshape() but will zero-pad the final vector if necessary to make it fit. Also, buffer() can do overlapping.
Thank you. reshape works perfectly as long as the vector is divisible by 400, which it is!

Categories

Community Treasure Hunt

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

Start Hunting!