divide matrix to be sub-matrix

1 vue (au cours des 30 derniers jours)
ha ha
ha ha le 12 Avr 2018
Modifié(e) : ha ha le 12 Avr 2018
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 commentaire
Adam
Adam le 12 Avr 2018
doc mat2cell
should help with this, although it is a function whose parameterisation always confuses me on the rare occasions I use it!

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 12 Avr 2018
Modifié(e) : Stephen23 le 12 Avr 2018
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 commentaire
ha ha
ha ha le 12 Avr 2018
Modifié(e) : ha ha le 12 Avr 2018
@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];

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by