How to split a row vector into sub vectors with different sizes?

Let h = [1 2 3 4 5 6 7 8 9 10 11]
n1= 3, n2= 3 , n3 = 2, n4= 2
how can I create sub vectors(m1,m2,m3,m4) based on the length of n1, n2 , n3 , n4 starting from the second elemnt of h. The resulting sub vectrs should be like this:
m1= 2 3 4
m2 = 5 6 7
m3= 8 9
m4= 10 11
Note that n1, n2 , n3 , n4 can be of different sizes.

1 件のコメント

Adam
Adam 2018 年 11 月 16 日
Don't even try to create individually named variables. The answer below puts them into 4 cells of a cell array which is much better.

サインインしてコメントする。

 採用された回答

Andrei Bobrov
Andrei Bobrov 2018 年 11 月 16 日

1 投票

h = [1 2 3 4 5 6 7 8 9 10 11];
n = [3, 3, 2, 2];
m = mat2cell(h(2:end),1,n)

4 件のコメント

madhan ravi
madhan ravi 2018 年 11 月 16 日
+1 elegant
Hajem Daham
Hajem Daham 2018 年 11 月 16 日
Thanks Andrei, sorry Iam not so good in matlab, could please complete the code until getting sub vectors m1,m2,m3,m4 separately like this:
m1= 2 3 4
m2 = 5 6 7
m3= 8 9
m4= 10 11
madhan ravi
madhan ravi 2018 年 11 月 16 日
celldisp(m)
Hajem Daham
Hajem Daham 2018 年 11 月 16 日
Thanks

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by