1
$\begingroup$

Given a matrix mat, partition it in the following format:

SeedRandom[123];
mat = RandomInteger[10, {20, 20}];

mat1 = Take[mat, 10, 10];
mat2 = Take[mat, 10, 11 ;; 13];
mat3 = Take[mat, 10, 14 ;; 20];

mat4 = Take[mat, 11 ;; 16, 10];
mat5 = Take[mat, 11 ;; 16, 11 ;; 13];
mat6 = Take[mat, 11 ;; 16, 14 ;; 20];

mat7 = Take[mat, 17 ;; 20, 10];
mat8 = Take[mat, 17 ;; 20, 11 ;; 13];
mat9 = Take[mat, 17 ;; 20, 14 ;; 20];

The original matrix can be rebuilt as follows:

ArrayFlatten[{{mat1, mat2, mat3}, {mat4, mat5, mat6}, {mat7, mat8, mat9}}]

After performing various calculations over the partitions of mat:

mat11 = mat1;
mat22 = Total[mat2, {2}];
mat33 = Total[mat3, {2}];

mat44 = Total[mat4];
mat55 = Total[mat5, 2];
mat66 = Total[mat6, 2];

mat77 = Total[mat7];
mat88 = Total[mat8, 2];
mat99 = Total[mat9, 2];

I want to build a reduced form of the matrix as:

ArrayFlatten[{{mat11, mat22, mat33}, {mat44, mat55, mat66}, {mat77, mat88, mat99}}]

It seems that I have problems with the dimensions of the reduced format. Take for example mat22, which is the sum of rows in the partitioned matrix mat2. It should have a dimension of (10, 1). But it has a dimension of 10 (a list). The same problems appear in other partitions also.

My question is how to format the reduced elements in such a way as to Append them consistently.

Put it another way, how to Append a (10,1) matrix to a (10, 10) matrix after a row-wise summing of (10,4) matrix.

$\endgroup$
4
  • 2
    $\begingroup$ "It should have a dimension of (10, 1)" I think you have the wrong expectation of what Total does. It's a reducing operator. Just like Total[{1,2,3}] returns 6 and not {6}. You'll just have to add the desired layers of list back in manually. E.g., List @ Total[mat, {1}] and List /@ Total[mat, {2}] $\endgroup$ Sep 15, 2023 at 14:32
  • $\begingroup$ @Sjoerd Smit: Thanks for your clarification. Isn't there an automatic way to do the manual work? Suppose I have a list of numbers (a vector) and want to represent it as a column matrix. What is the code for this? This will solve my problem. $\endgroup$ Sep 15, 2023 at 14:36
  • 1
    $\begingroup$ Note that MMA does not distinguish between column and row vectors. However, if you want to transform a vector: v into a nx1 matrix: ` v={1,2,3}; List/@v ` results in: {{1}, {2}, {3}} $\endgroup$ Sep 16, 2023 at 10:08
  • $\begingroup$ @Daniel Huber: Thanks for this tip. I did not know what List/@v does. $\endgroup$ Sep 16, 2023 at 19:29

1 Answer 1

1
$\begingroup$

Following @Sjoerd Smit's comments, I have the following answer to my question:

mat11 = mat1;
mat22 = List /@ Total[mat2, {2}];
mat33 = List /@ Total[mat3, {2}];

mat44 = List@Total[mat4, {1}];
mat55 = List /@ Total[mat5, 2];
mat66 = List /@ Total[mat6, 2];

mat77 = List@Total[mat7, {1}];
mat88 = List /@ Total[mat8, 2];
mat99 = List /@ Total[mat9, 2];

ArrayFlatten[{{mat11, mat22, mat33}, {mat44, mat55, mat66}, {mat77, mat88, mat99}}] // MatrixForm

generates:

enter image description here

Although I solve my problem, the answer looks pretty cluttered. I suppose there is a neat way to achieve a better result.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.