Cut vector into many new vectors with defined lengths

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

Accepted Answer

Star Strider
Star Strider on 29 May 2015
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
Joseph Cheng
Joseph Cheng on 29 May 2015
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
Walter Roberson
Walter Roberson on 29 May 2015
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)

Walter Roberson
Walter Roberson on 29 May 2015
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.

Kimberly S
Kimberly S on 1 Jun 2015
Thank you. reshape works perfectly as long as the vector is divisible by 400, which it is!

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!