How to split up column vector

14 ビュー (過去 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 Center および File ExchangeWhos についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by