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일
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개)

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 Exchange에서 Resizing 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