Split a matrix at specific row

12 ビュー (過去 30 日間)
Alex Grimaud
Alex Grimaud 2018 年 3 月 6 日
回答済み: Alex Grimaud 2018 年 3 月 12 日
Hello,
I have a 105x4 matrix and a vector [11 12 11 11 12 12 13 11 12] where the sum represent the row size of the matrix. I want to divide the matrix into several matrix, the first will start at row 1 to 11 and give a 11x4 matrix. The second will start a 12 to 24 and give a 12x4 matrix.. etc.. How can i do that? i'm a little bit lost
Thanks in advance for your help

採用された回答

Jos (10584)
Jos (10584) 2018 年 3 月 12 日
Use mat2cell:
M = randi(20, 105,4) ;
V = [11 12 11 11 12 12 13 11 12] ;
C = mat2cell(M, V , size(M,2)) ;

その他の回答 (2 件)

Akira Agata
Akira Agata 2018 年 3 月 12 日
Like this?
% Input matrix (A) and vector (h)
A = rand(105,4);
h = [11 12 11 11 12 12 13 11 12];
% Calculate start- and end-row index based on the given vector
endIdx = cumsum(h);
startIdx = [1 endIdx(1:end-1)+1];
% Separate the matrix
C = arrayfun(@(x,y) A(x:y,:),startIdx,endIdx,'UniformOutput',false);
The result looks like:
>> C
C =
1×9 cell array
{11×4 double} {12×4 double} {11×4 double} {11×4 double} {12×4 double} {12×4 double} {13×4 double} {11×4 double} {12×4 double}
  2 件のコメント
Jos (10584)
Jos (10584) 2018 年 3 月 12 日
Why so difficult? More than a decade ago, Matlab has created the function mat2cell to do exactly this ...
Akira Agata
Akira Agata 2018 年 3 月 12 日
編集済み: Akira Agata 2018 年 3 月 12 日
Yes, mat2cell has just slipped my mind. Thanks!

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


Alex Grimaud
Alex Grimaud 2018 年 3 月 12 日
Thanks for the reply, the two solution works perfectly. But mat2cell is more simple !

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by