How to split up column vector

15 次查看(过去 30 天)
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.

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