How to split up column vector

4 views (last 30 days)
audi23
audi23 on 1 Jun 2019
Commented: audi23 on 2 Jun 2019
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!

Accepted Answer

dpb
dpb on 1 Jun 2019
Edited: dpb on 1 Jun 2019
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!