How can I split a matrix into N different parts?
Mostrar comentarios más antiguos
I have a 120x30 matrix of data points, how can I split the matrix into nine equal parts of 400 data chunks and store them as separate matrices?
2 comentarios
"how can I split the matrix into nine equal parts of 400 data chunks and store them as separate matrices?"
While this is possible it would make your code slow, buggy, inefficient, hard to read, hard to debug, etc, etc. Here is what the MATLAB documentation says about magically creating variable names like that: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array."
As you can read, the much better alternative is use one array and indexing. Indexing is simple, very efficient, neat, easy to read, easy to debug, etc, etc.
You can read more explanations and discussions here:
Just use indexing: simple, efficient, neat, easy to debug, easy to understand,... Or you could split the array into a cell array of numeric arrays: see mat2cell and num2cell.
Respuesta aceptada
Más respuestas (2)
Storing in separate matrices might be a bad idea. Why not using the matrix itself?
data = rand(120, 30);
data9 = reshape(data, [], 9);
dataC = cell(1, 9);
for iC = 1:9
dataC{iC} = data9(:, iC);
end
Here you get the different vectors as a cell array. But I'd prefer to work with data9: The i.th block is data9(:, i).
[EDITED] You can split the matrix to a 3x3 set uf submatrices efficiently without nested loops by:
sM = size(M);
c = reshape(M, sM(1) / 3, 3, sM(2) / 3, 3);
M33 = permute(reshape(c, 4, 3, 3, 3), [1, 3, 2, 4]);
M9 = reshape(M33, [4, 3, 9]);
Now the matrix is divived in 3 x 3 submatrices of the same size, which can be addressed by M9(:, :, i) with the index 1 to 9 or by using M33(:, :, x, y) with x and y going from 1 to 3.
This is much faster then nested loops.
2 comentarios
Kodavati Mahendra
el 17 de En. de 2018
Editada: Kodavati Mahendra
el 17 de En. de 2018
regarding the edit you made, c has more than 4*3*3*3 elements.
sM = size(M);
M33 = permute(reshape(M,sM(1)/3,3,sM(2)/3,3),[1,3,2,4]);
M9 = reshape(M33, [sM(1)/3,sM(2)/3,9]);
Is this the correct code or did I misunderstand it?
Jan
el 17 de En. de 2018
@Kodavati Mahendra: This looks fine.
Image Analyst
el 22 de Jul. de 2017
You didn't specify how you wanted the matrix split up, since neither 120 nor 30 is a multiple of 9. So I just made 9 vectors taking the data column by column from upper left to lower right. Try this:
data = rand(120,30); % Sample data
% Now split up into 9 row vectors.
m1 = data(1:1*400);
m2 = data(1*400+1 : 2*400);
m3 = data(2*400+1 : 3*400);
m4 = data(3*400+1 : 4*400);
m5 = data(4*400+1 : 5*400);
m6 = data(5*400+1 : 6*400);
m7 = data(6*400+1 : 7*400);
m8 = data(7*400+1 : 8*400);
m9 = data(8*400+1 : 9*400);
If those aren't the 9 arrays you were thinking of them clarify the location of where the elements for each matrix are supposed to come from.
Categorías
Más información sobre Creating and Concatenating 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!




