divide matrix to be sub-matrix

1 回表示 (過去 30 日間)
ha ha
ha ha 2018 年 4 月 12 日
編集済み: ha ha 2018 年 4 月 12 日
A is matrix:
A=[111;222;333;444;555;666;777;888;999;1000;2000]; %total number of A is 11
I wanna divide matrix A to be "n" matrix with the equal number of each matrix :
Example (with n=4), the result is:
A1=[111;222;333]; % contain 3 element
A2=[444;555;666]; % contain 3 element
A2=[777;888;999]; % contain 3 element
A2=[1000;2000]; % contain 2 element
In this example: 11/4=2.75, so choose the number of each matrix is 3. And the number of element in the last matrix will be the remaining
And, then, store the resulted matrix in cell:
A_cell={A1, A2, A3, A4}
How to get:
A_cell={[111;222;333] ,[444;555;666], [777;888;999], [1000;2000] }
  1 件のコメント
Adam
Adam 2018 年 4 月 12 日
doc mat2cell
should help with this, although it is a function whose parameterisation always confuses me on the rare occasions I use it!

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

採用された回答

Stephen23
Stephen23 2018 年 4 月 12 日
編集済み: Stephen23 2018 年 4 月 12 日
Method one: mat2cell:
mat2cell(A,[3,3,3,2])
You can generate the second input like this:
>> G = 4; % pick number of groups
>> A = [111;222;333;444;555;666;777;888;999;1000;2000];
>> L = numel(A);
>> N = ceil(L/G); % max elements per group
>> V = [N*ones(1,G-1),1+mod(L-1,N)];
>> C = mat2cell(A,V);
>> C{:}
ans =
111
222
333
ans =
444
555
666
ans =
777
888
999
ans =
1000
2000
Method two: accumarray:
>> V = ceil((1:L)/N);
>> C = accumarray(V(:),A,[],@(v){v});
>> C{:}
ans =
111
222
333
ans =
444
555
666
ans =
777
888
999
ans =
1000
2000
  1 件のコメント
ha ha
ha ha 2018 年 4 月 12 日
編集済み: ha ha 2018 年 4 月 12 日
@Thanks Stephen Cobeldick. If I wanna find the maximum value of each matrix from cell C. How can I do?
result=[333 666 999 2000];

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by