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).

 采纳的回答

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 个评论

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).

请先登录,再进行评论。

更多回答(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!

类别

帮助中心File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by