divide matrix to be sub-matrix

1 Ansicht (letzte 30 Tage)
ha ha
ha ha am 12 Apr. 2018
Bearbeitet: ha ha am 12 Apr. 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 Kommentar
Adam
Adam am 12 Apr. 2018
doc mat2cell
should help with this, although it is a function whose parameterisation always confuses me on the rare occasions I use it!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 12 Apr. 2018
Bearbeitet: Stephen23 am 12 Apr. 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 Kommentar
ha ha
ha ha am 12 Apr. 2018
Bearbeitet: ha ha am 12 Apr. 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];

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Numeric Types finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by