1
$\begingroup$

Suppose that I have the following matrix:

matrix = Table[i+j, {i, 5}, {j, 0, 4}]; 

which generates

enter image description here

I like to write a Mathematica function such as mm[matrix_] to conduct linear operations using rows and columns. For example, summing 1st and 4th columns and summing 2nd row with 5th row. The resulting function should be obtained using mm[matrix_]. The position of the new column and row is not important for my purpose.

The output should be:

{{2 3 5 5}, {9 11 20 15}, {4 5 9 7}, {5 6 11 8}} 

Thanks.

Tugrul

$\endgroup$
1
  • $\begingroup$ matrix[[2]] + matrix[[5]] sums the second and fifth rows. You might want to look at the instruction Part[ ] which is what is being used by the double brackets. $\endgroup$ Commented Mar 5, 2019 at 0:20

2 Answers 2

1
$\begingroup$
ClearAll[f] f = Transpose @ Fold[Module[{m = Transpose@#, x = #2}, ReplacePart[m, Join[#[[1]] -> Plus @@ m[[#]] & /@ x, {Alternatives @@ Flatten[x[[All, 2 ;;]]] -> Sequence[]}]]] &, #, {##2}] &; TeXForm @ MatrixForm @ f[matrix, {{2, 5}}, {{4, 1}}] 

$\left( \begin{array}{cccc} 2 & 3 & 5 & 5 \\ 9 & 11 & 20 & 15 \\ 4 & 5 & 9 & 7 \\ 5 & 6 & 11 & 8 \\ \end{array} \right)$

matrix2 = Partition[Range[81], 9]; TeXForm @ MatrixForm @ f[matrix2, {{2, 5, 1}, {3, 4}}, {{4, 1, 5}, {7, 8, 9}}] 

$\left( \begin{array}{ccccc} 35 & 62 & 213 & 143 & 591 \\ 25 & 43 & 147 & 97 & 399 \\ 15 & 24 & 81 & 51 & 207 \\ 16 & 25 & 84 & 52 & 210 \\ 17 & 26 & 87 & 53 & 213 \\ 18 & 27 & 90 & 54 & 216 \\ \end{array} \right)$

Also:

ClearAll[f2] f2 = Module[{m = #}, m[[#2[[1]]]] += m[[#2[[2]]]]; m[[All, #3[[1]]]] += m[[All, #3[[2]]]]; Drop[m, {#2[[2]]}, {#3[[2]]}]] &; f2[matrix, {2, 5}, {4, 1}] == f[matrix, {{2, 5}, {4, 1}}] 

True

$\endgroup$
14
  • $\begingroup$ Yes, it works fine. Can we extend your Code to do operations on more than 2 columns and more than 2 rows. My example was for illustrative purposes, and in reality I have a matrix of 60 by 60, and need to reduce its dimension to 25 by 25. Thank you for your answer. $\endgroup$ Commented Mar 5, 2019 at 1:27
  • 1
    $\begingroup$ @TugrulTemel, I updated f to work with arbitrary number of rows and columns. $\endgroup$ Commented Mar 5, 2019 at 1:54
  • 1
    $\begingroup$ @Tugrul, updated f to take lists of lists of rows (and columns) to be combined. $\endgroup$ Commented Mar 6, 2019 at 7:24
  • 1
    $\begingroup$ Tugrul, if the row (or column) to be moved is not necessarily one of the rows (columns) that appear in the arguments of f, then it is probably a good idea to post as a new question. If it is, than you can re-shuffle the list of rows (say, {2,3,4}) to be combined so that the target position (say, 4) appears first (that is, use {4,2,3}). Similarly for the list of of columns to be combined. $\endgroup$ Commented Mar 12, 2019 at 22:45
  • 1
    $\begingroup$ @Tugrul, for the second case this may be useful. $\endgroup$ Commented Mar 12, 2019 at 23:41
0
$\begingroup$

There's nothing special about matrix functions. Something like:

r2p5[matrix_]:= matrix[[2]] + matrix[[5]] 

yields the sum of rows 2 and 5 of whatever matrix you feed it.

All r2p5[something] does is:

Evaluates something.

Takes the result of that and substitutes it for matrix in the expression matrix[[2]] + matrix[[5]]

Evaluates the resulting expression.

$\endgroup$
2
  • $\begingroup$ My question was not completely clear and has some missing information. Suppose I have 5 by 5 matrix. Sum 2nd and 5th columns and create a new column 'c25' and do the same sum on row and create a new row 'r25'. By doing this operation we reduce the size of the original matrix to 4 by 4. I wanted to do the entire operation by a matrix function and get the final reduced matrix of 4 by 4, which is the output form in my question. $\endgroup$ Commented Mar 5, 2019 at 1:00
  • $\begingroup$ You said above you knew how to do this, but not in a function. But if you know how to do it, you know how to do it in a function, it's the same. $\endgroup$ Commented Mar 5, 2019 at 1:09

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.