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

 採用された回答

Star Strider
Star Strider 2015 年 5 月 29 日

0 投票

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 件のコメント

Joseph Cheng
Joseph Cheng 2015 年 5 月 29 日
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 2015 年 5 月 29 日
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 件)

Walter Roberson
Walter Roberson 2015 年 5 月 29 日

0 投票

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 2015 年 6 月 1 日

0 投票

Thank you. reshape works perfectly as long as the vector is divisible by 400, which it is!

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

質問済み:

2015 年 5 月 29 日

回答済み:

2015 年 6 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by