how can partition a vector into smaller sub sequences

11 ビュー (過去 30 日間)
hanadi abbas
hanadi abbas 2021 年 4 月 7 日
コメント済み: hanadi abbas 2021 年 4 月 7 日
Hi,
how can partition a vector into smaller sequences: for example, let A is a vector of 179901, N is another vector consists of 204 elements. I want to partition A into several sub sequences according to values of vector N such that
seq1=A(1:N(1))
seq2=A(1+N(1):N(1)+N(2))
seq3=A(1+N(1)+N(2):N(1)+N(2)+N(3))
and so on for other sequences
how can do this, I use MatlabR2017a. Thanks in advance

採用された回答

Stephen23
Stephen23 2021 年 4 月 7 日
編集済み: Stephen23 2021 年 4 月 7 日
A = 1:19;
N = [3,5,7];
S = mat2cell(A(1:sum(N)),1,N)
S = 1×3 cell array
{[1 2 3]} {[4 5 6 7 8]} {[9 10 11 12 13 14 15]}
Or
V = cumsum([0,N]);
F = @(b,e)A(1+b:e);
S = arrayfun(F,V(1:end-1),V(2:end),'uni',0)
S = 1×3 cell array
{[1 2 3]} {[4 5 6 7 8]} {[9 10 11 12 13 14 15]}

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by