Divide Matrix into smaller matrices
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi, I have a problem regarding a matrice into smaller matrices. Basically I have a Matrix A (1000 x 2 double) which I want to subdivide into matrices with the dimension of 20 x 2. Thereby I want to maintain the value pairs in the matrices. Maybe this helps to understand what I Mean Original Matrix:
A = a1,1 a1,2
a2,1 a2,2
... ...
a1000,1 a1000,2
After the transformation the resulting matrices should look like this:
A_new1 = a1,1 a1,2
a2,1 a2,2
... ...
a20,1 a20,2
A_new2 = a21,1 a21,2
a22,1 a22,2
... ...
a40,1 a40,2
until
A_new50 = a981,1 a981,2
a982,1 a982,2
... ...
a1000,1 a1000,2
I tried to use to use reshape, but was not successful. Maybe somebody has an idea. Thanks in advance.
0 comentarios
Respuestas (2)
Sumukh
el 30 de Jul. de 2024
Hi Max.
To divide the matrix into multiple matrices, please check out the following code:
arr_temp=1:2000; % To store the 2000 numbers, taking 1:2000 numbers for easy understanding.
arr_1000_2=reshape(arr_temp,[1000,2]); % Reshaping the date into array of size 1000x2.
arr_20_2=zeros(20,2); % Declaring an array of size 20x2 to display the arrays
% For-loop to display the 20x2 arrays derived from 1000x2 array:
for i=1:20:1000 % 1000 rows, leaping 20 at a time
arr_20_2=arr_1000_2(i:i+20-1,:); % Accessing every group of 20 rows from beginning and two column data.
% disp(arr_20_2+"\n"); % Any post-processing on the 20x2 array can be done here.
end
disp(arr_20_2); % Just displaying the last matrix, as output is too long.
Array-indexing using the “:” operator allows us to access multiple rows and columns at a time. The above code uses a for-loop to iterate through the big matrix and divide it into smaller matrices. Any post-processing for the smaller matrices can be done within the loop itself, by replacing the commented out “disp” function:
% disp(arr_20_2+"\n"); % Any post-processing……
More about the “reshape” function can be found here:
I hope this resolves your difficulty.
0 comentarios
Walter Roberson
el 30 de Jul. de 2024
A_cell = mat2cell(A1000_Matrix, 20*ones(1,size(A1000_Matrix,1)/20)), size(A1000_Matrix,2));
Now access A_cell{1}, A_cell{2} and so on.
0 comentarios
Ver también
Categorías
Más información sobre Operators and Elementary Operations en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!