divide matrix to be sub-matrix

1 visualización (últimos 30 días)
ha ha
ha ha el 12 de Abr. de 2018
Editada: ha ha el 12 de Abr. de 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 comentario
Adam
Adam el 12 de Abr. de 2018
doc mat2cell
should help with this, although it is a function whose parameterisation always confuses me on the rare occasions I use it!

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 12 de Abr. de 2018
Editada: Stephen23 el 12 de Abr. de 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 comentario
ha ha
ha ha el 12 de Abr. de 2018
Editada: ha ha el 12 de Abr. de 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];

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by