How do I split a vector into parts using indexes?

51 ビュー (過去 30 日間)
David Haydock
David Haydock 2022 年 10 月 4 日
回答済み: Stephen23 2022 年 10 月 4 日
I have a vector, say
x = 1:100;
and I have a vector that is indexes within the range of the size of x, say,
y=[10,40,60];
I want to cut up x so that I get the three vectors:
x1 = x(1:y(1));
x2 = (y(1)+1:y(2));
x3 = (y(2)+1:y(3));
I tried to use a loop but there is indexing errors due to Matlab indexing starting at 1.
Help appreciated.

採用された回答

Stephen23
Stephen23 2022 年 10 月 4 日
x = 1:100;
y = [10,40,60];
C = mat2cell(x(1:y(end)),1,diff([0,y]))
C = 1×3 cell array
{[1 2 3 4 5 6 7 8 9 10]} {[11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40]} {[41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60]}

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by