Split big matrix in many submatrices having same size
Mostrar comentarios más antiguos
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 comentarios
I have a big matrix 396*2600 and I want to split it to many matrices 30*120 as follow
Seems like a bad idea. That will consume approximately 25GB of RAM if you store the results as doubles:
30*120*(396-(30-1))*(2600-(120-1))*8/2^30
Are you sure you aren't just trying to do a convolution? There are efficient function for that.
Mehdi Kooli
el 7 de Nov. de 2022
Stephen23
el 7 de Nov. de 2022
"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. "
Then you should ask about that, not about your attempted anti-pattern solution:
Explain clearly what your actual goal is. Do you have the image processing toolbox?
Mehdi Kooli
el 7 de Nov. de 2022
Mehdi Kooli
el 23 de Nov. de 2022
Movida: Matt J
el 23 de Nov. de 2022
Mehdi Kooli
el 9 de Dic. de 2022
Movida: Matt J
el 9 de Dic. de 2022
Respuestas (2)
John D'Errico
el 4 de Nov. de 2022
Editada: John D'Errico
el 4 de Nov. de 2022
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)
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)
There are 12 such sub-arrays.
B(:,:,1)
B(:,:,2)
B(:,:,12)
5 comentarios
Mehdi Kooli
el 7 de Nov. de 2022
Movida: Stephen23
el 7 de Nov. de 2022
Mehdi Kooli
el 10 de Nov. de 2022
"I have an error : Error using + Matrix dimension must agree"
John D'Errico's answer uses explicit arithmetic expansion, which was introduced in R2016b:
Your MATLAB version (R2015b) does not support explicit arithmetic expansion, so you need to replace the subtraction operations(and possibly others) with BSXFUN() and MINUS().
Mehdi Kooli
el 12 de Dic. de 2022
Jilin Zhang
el 21 de Dic. de 2022
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
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.
Categorías
Más información sobre Resizing and Reshaping Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!