필터 지우기
필터 지우기

Split big matrix in many submatrices having same size

조회 수: 7 (최근 30일)
Mehdi Kooli
Mehdi Kooli 2022년 11월 4일
댓글: Jilin Zhang 2022년 12월 21일
Hello,
I have a big matrix 396*2600 and I want to split it to many matrices 30*120 as follow : (let's consider this exemple on a small matrix A ans split it to 3*2 matrices):
A = [1 9 17
2 10 18
3 11 19
4 12 20
5 13 21
6 14 22
7 15 23
8 16 24];
results wanted: A1 = [1 9
2 10
3 11]
A2 = [9 17
10 18
11 19]
A3 = [2 10
3 11
4 12]
A3 = [10 18
11 19
12 20] and so on ...
Remarks: I need a solution without for loop, I'm looking for a Matlab command giving this results.
Thanks
  댓글 수: 6
Mehdi Kooli
Mehdi Kooli 2022년 11월 23일
이동: Matt J 2022년 11월 23일
Hello Andrei Bobrov, I saw you answered similar question (https://fr.mathworks.com/matlabcentral/answers/357482-reshaping-a-2d-matrix-into-a-3d-matrix-layers-of-square-matrices-with-specific-order-no-loop?s_tid=srchtitle)
Can you please take a look here , maybe you have the solution.
Mehdi Kooli
Mehdi Kooli 2022년 12월 9일
이동: Matt J 2022년 12월 9일
Hello @Andrei Bobrov maybe you have the answer ?

댓글을 달려면 로그인하십시오.

답변 (2개)

John D'Errico
John D'Errico 2022년 11월 4일
편집: John D'Errico 2022년 11월 4일
So many times this gets asked for. DON'T DO IT. Instead, learn to use arrays, of many types. For example, just make it into a 3 dimensional array, where each plane of that array is one of the desired sub-arrays. That requires relatively little more than understanding how to index arrays.
A = [1 9 17
2 10 18
3 11 19
4 12 20
5 13 21
6 14 22
7 15 23
8 16 24];
Here you want to generate all 3x2 contiguous subarrays. The size of A is
[r,c] = size(A)
r = 8
c = 3
So there will be 6*2 such 3x2 sub-arrays to generate.
Where are the elements of A stored in memory? In what order? Understanding this, and how tools like sub2ind work in MATLAB allows you to build these arrays easily.
ind1 = (1:3)' + [0,r];
ind2 = (1:r-2) + r*[0:c-2]';
B = reshape(A(ind1(:)-1 + ind2(:)'),3,2,[]);
Now it is simple to acces any of those subarrays as we have created, and do so programmatically. The array B is of size:
size(B)
ans = 1×3
3 2 12
There are 12 such sub-arrays.
B(:,:,1)
ans = 3×2
1 9 2 10 3 11
B(:,:,2)
ans = 3×2
9 17 10 18 11 19
B(:,:,12)
ans = 3×2
14 22 15 23 16 24
  댓글 수: 5
Mehdi Kooli
Mehdi Kooli 2022년 12월 12일
@Stephen23 Thanks a lot, it resolves the problem for this example but I need to adapt the code for a big matrix A= 426 x 2904 using a sliding window 60 x 240
I don't understand the value 2 in the code (see comments below)
Here is the code :
rect_l=3; % rows lenght of my sliding window
rect_c=2; % columns lenght of my sliding window
% Matrix example 8 x 3
A = [1 9 17
2 10 18
3 11 19
4 12 20
5 13 21
6 14 22
7 15 23
8 16 24];
[r,c] = size(A);
x1 = (1:rect_l)';
y1 = [0,r];
ind1 = bsxfun(@plus,x1,y1);
x2 = (1:r-2); % Why the value 2 ?
y2 = r*[0:c-2]'; % Why the value 2 ?
ind2 = bsxfun(@plus,x2,y2);
ind3 = bsxfun(@minus,ind1(:),1);
ind4 = ind2(:)';
ind5 = bsxfun(@plus,ind3,ind4);
B = reshape(A(ind5),rect_l,rect_c,[]);
Can you please tell me how to modify the code for the new Matrix size and sliding window.
Jilin Zhang
Jilin Zhang 2022년 12월 21일
A=randi([0 10],426,2904)
[r1, c1]=size(A);
r2=60 %target row #
c2=240 %target column #
i=1
j=1
k=1
while i*r2<r1
while j*c2<c1
B(k,:,:)=A((i-1)*r2+1:i*r2,(j-1)*c2+1:j*c2);
j=1+j
k=1+k
end
i=1+i
end

댓글을 달려면 로그인하십시오.


Matt J
Matt J 2022년 11월 7일
편집: Matt J 2022년 11월 7일
What I'm trying to do is to split the matrix to submatrices and then for each submatrix, calculate the median value and store the result in another matrix.
The problem simplifies greatly if you choose a submatrix size with odd dimensions, in which case you can use medfilt2. Otherwise, ordfilt2 would allow you the increased flexibility of using any dimensions.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by