How to split up column vector

조회 수: 15 (최근 30일)
audi23
audi23 2019년 6월 1일
댓글: audi23 2019년 6월 2일
Say I have a long column vector, I now need to split up the vector into smaller vectors according to sizes given in another vector. For instance, say I want the sizes to be [14;11;51; etc...] I want my main vector to be split up into vectors of sizes 14, 11, 51, etc... and in the same order as my main matrix.
Thanks in advance!

채택된 답변

dpb
dpb 2019년 6월 1일
편집: dpb 2019년 6월 1일
In general, if you're thinking of starting with vector V and creating subvectors, say, A, B, C, ..., this is a very bad idea. It creates a real mess to be able to refer to those variables later.
To segregate data this way and make reference to it programmatically simple, use something like
ix=[14;11;51]; % define the breakpoint vector
v=1:sum(ix);v=v(:); % create some dummy data of right size
>> mat2cell(v,ix) % create cell array of each group
ans =
3×1 cell array
{14×1 double}
{11×1 double}
{51×1 double}
>>
Or, another approach is to create an auxiliary grouping variable and use findgroups and splitapply or similar techniques to process by group that doesn't require explicitly building the other results as variables.
  댓글 수: 1
audi23
audi23 2019년 6월 2일
Perfect, thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by