4
$\begingroup$

I've been struggling with one problem. I have a classic matrix in this example 12x4 such as: matrix = Table[i, {i, 12}, {4}]

TableForm@matrix

I need to sum the rows according to the example below.

Total@matrix[[{1, 5, 9}]]

Total@matrix[[{2, 6, 10}]]

Total@matrix[[{3, 7, 11}]

I tried using MapAt, but without any real success so far. I need to make a function out of it, cause later I will be using it for matrix 1036x37. Any tips?

$\endgroup$
1
  • 1
    $\begingroup$ Why not contain {4,8,12}? $\endgroup$
    – cvgmt
    Dec 8 '20 at 0:16
1
$\begingroup$

You can use SparseArray to create a "summing" matrix, and use Dot:

SparseArray[{Band[{1, 1}] -> 1, Band[{1, 5}] -> 1, Band[{1, 9}] -> 1}, {3, 12}] . matrix

{{15, 15, 15, 15}, {18, 18, 18, 18}, {21, 21, 21, 21}}

$\endgroup$
1
$\begingroup$
Clear["Global`*"]

sum[matrix_?MatrixQ, start_Integer?Positive, step_Integer?Positive] :=
  Total@matrix[[start ;; ;; step]]

matrix = Table[i, {i, 12}, {4}];

sum[matrix, 1, 4]

(* {15, 15, 15, 15} *)

sum[matrix, 2, 4]

(* {18, 18, 18, 18} *)

sum[matrix, 3, 4]

(* {21, 21, 21, 21} *)

Or

sum[matrix, #, 4] & /@ Range[3]

(* {{15, 15, 15, 15}, {18, 18, 18, 18}, {21, 21, 21, 21}} *)
$\endgroup$
1
$\begingroup$
ClearAll[f1]
f1[m_] :=  Total[Transpose @ Partition[m, Dimensions[m][[2]]], {2}]

f1 @ matrix
{15, 15, 15, 15}, {18, 18, 18, 18}, {21, 21, 21, 21}, {24, 24, 24, 24}}}

Inspired by Carl's answer, an alternative way to use Dot:

ClearAll[f2]
f2[m_] :=  Module[{d = Dimensions @ m}, 
  PadRight[IdentityMatrix[Last @ d], Reverse @ d, "Periodic"].m]

f2@matrix
{{15, 15, 15, 15}, {18, 18, 18, 18}, {21, 21, 21, 21}, {24, 24, 24, 24}}
$\endgroup$
1
$\begingroup$

Here we set n=14 instead of n=12 to test the method.

n = 14;
matrix = Table[i, {i, n}, {4}];
d = 4;
m = 3;
index = Table[i + d (j - 1), {i, n + d - d*m}, {j, m}]
Total[matrix[[#]]] & /@ index

{{1, 5, 9}, {2, 6, 10}, {3, 7, 11}, {4, 8, 12}, {5, 9, 13}, {6, 10, 14}}

{{15, 15, 15, 15}, {18, 18, 18, 18}, {21, 21, 21, 21}, {24, 24, 24, 24}, {27, 27, 27, 27}, {30, 30, 30, 30}}

$\endgroup$
1
$\begingroup$

Try with the following code:

RowsSum[nmax_Integer?Positive, length_Integer?Positive, vector_List] := Module[
   {matrix, matrixrows, s},
matrix = Table[i, {i, nmax}, {length}]; 
matrixrows[vector2_List, matrix_] := matrix[[vector]]; 
s = Flatten@Map[Total, List[matrixrows[vector, matrix]]];
Return[s];
];

Test:

RowsSum[12, 4, {1, 5, 9}]
(*{15, 15, 15, 15}*)
RowsSum[12, 4, {2, 6, 10}]
(*{18, 18, 18, 18}*)
RowsSum[12, 4, {3, 7, 11}]
(*{21, 21, 21, 21}*)

With conditions:

RowsSum[nmax_Integer?Positive, length_Integer?Positive,condition_] := Module[
{matrix, vector, matrixrows, s},
matrix = Table[i, {i, nmax}, {length}]; 
vector = Select[Table[i, {i, Length[matrix]}], condition]; 
matrixrows[vector2_List, matrix_] := matrix[[vector]]; 
s = Flatten@Map[Total, List[matrixrows[vector, matrix]]];
Return[s];
];

Test:

RowsSum[12, 4, OddQ]
(*{36, 36, 36, 36}*)
RowsSum[12, 4, EvenQ]
(*{42, 42, 42, 42}*)
RowsSum[12, 4, 5 < # < 10 &]
(*{30, 30, 30, 30}*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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