Can I partition a matrix ito several seperated parts ?

can I partition this matrix into 4 parts ?
assume this matrix 29X1, A = [2;2;-3;4;5;6;7;-8;9;-6;5;4;-2;1;3;-9;8;7;4;-5;6;3;2;-1;4;-7;-8;5;-6];
so, I want to group this matrix into 4 seperated groups such as B=7X1, C=7X1, D=7X1, and E=8X1 ?

 Respuesta aceptada

Voss
Voss el 31 de Mzo. de 2022
A = [2;2;-3;4;5;6;7;-8;9;-6;5;4;-2;1;3;-9;8;7;4;-5;6;3;2;-1;4;-7;-8;5;-6];
% maybe this:
B = A(1:7);
C = A(8:14);
D = A(15:21);
E = A(22:29);
% or maybe this:
B = A(2:4:end);
C = A(3:4:end);
D = A(4:4:end);
E = A(1:4:end);
% etc., many other partitionings are possible

4 comentarios

omar th
omar th el 31 de Mzo. de 2022
first, Thank you for your reply
But, what do you think if the generation of matrix A is related to something random for example, when I ran the code, matrix A is 29x1,,, maybe when I want to run the code for the second time, A matrix will be 35x1,,,so, number of columns is fixed =1, but number of rows can be change.
is there a way to work on that assumption ?
omar th
omar th el 1 de Abr. de 2022
this worked
B = A(2:4:end);
C = A(3:4:end);
D = A(4:4:end);
E = A(1:4:end);
thank you so much
Ah, I don't think there was any way any of us could have guessed you wanted to select in that order.
omar th
omar th el 1 de Abr. de 2022
actually its not exactly in that order for sure i changed it, but this approach worked in my assumption
thanks again

Iniciar sesión para comentar.

Más respuestas (1)

A = [2;2;-3;4;5;6;7;-8;9;-6;5;4;-2;1;3;-9;8;7;4;-5;6;3;2;-1;4;-7;-8;5;-6];
parts = mat2cell(A, [7, 7, 7, 8], 1);
[B, C, D, E] = parts{:};
whos
Name Size Bytes Class Attributes A 29x1 232 double B 7x1 56 double C 7x1 56 double D 7x1 56 double E 8x1 64 double parts 4x1 648 cell

3 comentarios

omar th
omar th el 31 de Mzo. de 2022
Thank you for your reply, but as I mentioned above,
what do you think if the generation of matrix A is related to something random for example, when I ran the code, matrix A is 29x1,,, maybe when I want to run the code for the second time, A matrix will be 35x1,,,so, number of columns is fixed =1, but number of rows can be change.
is there a way to work on that assumption ? I mean a way can be adaptive with the given matrix
Your desired outcome is not defined. Do you always want 4 blocks, and the first three of them should be equal, and the last should absorb any extra capacity ?
A = randi([-9 9], 35, 1);
nrow = size(A,1);
each = floor(nrow/4);
parts = mat2cell(A, [each, each, each, nrow - 3*each], 1);
[B, C, D, E] = parts{:};
whos
Name Size Bytes Class Attributes A 35x1 280 double B 8x1 64 double C 8x1 64 double D 8x1 64 double E 11x1 88 double each 1x1 8 double nrow 1x1 8 double parts 4x1 696 cell
That gives 8, 8, 8, 11.
But perhaps you would instead prefer 9, 9, 9, 8 -- in which the last matrix might be shorter, but the distribution is more even.
omar th
omar th el 1 de Abr. de 2022
thank you so much

Iniciar sesión para comentar.

Productos

Etiquetas

Preguntada:

el 31 de Mzo. de 2022

Comentada:

el 1 de Abr. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by