Divide Matrix into smaller matrices
36 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Sumukh
2024-7-30,5:31
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 个评论
Walter Roberson
2024-7-30,6:50
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!