MATLAB: Categorizing matrix into submatrices, with specific elements of column

matrixsubmatrix

– Hope to categorize the matrix into number of 2D submatrices, with respect to elements of 6th and 7th column.
– Given matrix A is 2900 x 7 size, contains
[x_coordinate y_coordinate z_coordinate strain_energy atom_index x_category y_category].
Each line contains the information of each 2900 atoms. x_category is the column contains the numbers 1~20, y_category is the column contains the numbers 1~17
All columns contains the numbers.
Hope to change this
submatrix (1,1) = [x_coordinate y_coordinate z_coordinate strain_energy atom_index 1 1]
submatrix (1,2) = [x_coordinate y_coordinate z_coordinate strain_energy atom_index 1 2]
submatrix (20,17) = [x_coordinate y_coordinate z_coordinate strain_energy atom_index 20 17]
Number of rows of each submatrices differs, because
I've tried to use double for loop, but does not work.
for i = 1:20
for j = 1:17
submatrix_('i','j') = A(x_category==i & y_category==j,:);
end
end
But it is not workin...
Is there any way to split matrix into submatrices with specific conditions?

Best Answer

How about this (untested):
column6 = A(:, 6);
for x_category = 1 : 20
rowsWithThisX = (column6 == x_category);
submatrix6{x_category} = A(rowsWithThisX, :);
end
Same concept for the column 7
column7 = A(:, 7);
for y_category = 1 : 17
rowsWithThisY = (column6 == y_category);
submatrix7{y_category} = A(rowsWithThisY, :);
end
Adapt as needed.
Related Question