Skip to main content
10-Marble
October 5, 2022
Solved

Dividing a Matrix into Multiple matrices

  • October 5, 2022
  • 1 reply
  • 3805 views

Hi team, I have a column matrix with 98400 rows. I want to divide this matrix into multiple matrices with 25 elements in it each. So 3936 matrices. And want to store all the 3936 matrices in a variable, so that I can call each matrix with indexing. 
For example I have Matrix C with 1 column and 98400 rows. I want to divide C matrix into 3936 matrices, and also able to call each C1, C2..C3936 matrices with 25 elements each.
I am trying to do this using for loop but there has been no success.
In the attached example I have given a column matrix with 50 elements. and want to work out to divide that into 2 matrices.
Thanks

Best answer by Werner_E

Here is a utility function which splits a Matrix into n smaller ones

Werner_E_0-1665015675330.png

and here is the same again with the difference, that the second argument is the maximum number of elements per smaller matrix.

Werner_E_1-1665015749683.png

To split your matrix with 50 elements into two smaller ones with 25 elements each you would either use split(Column,2) or split2(Column,25).

Here are other examples

Werner_E_2-1665015886170.png

No error checking implemented (n larger than number of rows in M, negative or floating point arguments, ...)

 

 

1 reply

Werner_E25-Diamond IAnswer
25-Diamond I
October 6, 2022

Here is a utility function which splits a Matrix into n smaller ones

Werner_E_0-1665015675330.png

and here is the same again with the difference, that the second argument is the maximum number of elements per smaller matrix.

Werner_E_1-1665015749683.png

To split your matrix with 50 elements into two smaller ones with 25 elements each you would either use split(Column,2) or split2(Column,25).

Here are other examples

Werner_E_2-1665015886170.png

No error checking implemented (n larger than number of rows in M, negative or floating point arguments, ...)

 

 

KAL_162510-MarbleAuthor
10-Marble
October 6, 2022

Thanks for letting me know that there is a function to split a matrix. I was not aware of that. It will be really helpful for me to know in future problems, but I was looking for a different sort of solution. Or maybe I was not clear with what I wanted as a solution.  I was able to work through it and solve the problem using for loop. Here is the screenshot of what the loop looks like. It divides the one column matrix into 2 different matrix with same number of elements.

 

KAL_1625_1-1665020169016.png

 

25-Diamond I
October 6, 2022

if you look at the two "split" functions I posted you should see that they are doing exactly what you demand, just, more generic and not limited to just two smaller matrices.

Try sol1:=split(D,2) or sol2:=split2(D,25) and you will see that both sol1  and sol2 are identical to your solution.