3
$\begingroup$

This is second and important part of question(previous one), but I will make it self contained.

We have a matrix say MLarge and we want to add two matrices to it at special positions, described by the conditions.
We only want to make changes at respective positions(not all), like numbers at $(i,j),$ and $(i,j+2),$ $(i+2,j)$ in MLarge should be changed with matrix. e.g below

When we want to specially add two different matrices(containing variables) at different locations,
For example:

MLarge:= Table[i KroneckerDelta[i,j], {i, 1, 7}, {j, 1, 7}];

MLarge := $\left( \begin{array}{ccccccc} 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 2 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 3 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 4 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 5 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 6 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 7\\ \end{array} \right)$,

Mdiag[x_,y_] := $\left( \begin{array}{cc} 0 & x\\ y & 0 \\ \end{array} \right)$, Mnondiag[j_,k_] := $\left( \begin{array}{cc} 0 & j\\ k & 0 \\ \end{array} \right)$

Mdiag to be added to the MLarge on the diagonals and Mnondiag at $(i,j+2)$ and $(i+2,j)$.

They should look like this:
Mfinal[x_,j_,y_,k_] := $\left( \begin{array}{ccccccccccccc} 1+0 & x & 0 & 0 & (0 & j) & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ y & 0 & 0 & 0 & (k & 0) & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 2 & x & 0 & 0 & 0 & j & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & y & 0 & 0 & 0 & k & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 3 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 4 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 5 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 6 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 7\\ 0 & 0 & 0 & 0 & 0 & 0 & 0\\ \end{array} \right)$

Here, just first two(or four in the Mfinal) rows, indicating the case, I am interested in.
A general formalism will be of great use, general means adding matrix or matrices(with variables as above) at any desired location.
Any help will be highly appreciable, as I am new I forgive for asking obvious questions.

$\endgroup$
  • $\begingroup$ you mean $(i,i+2)$ and $(i+2, i)$ instead of $(i,j+2)$ and $(j+2, i)$? $\endgroup$ – kglr Jan 16 '17 at 19:59
  • $\begingroup$ Yes, and you implemented it also. Thanks for your advice of posting it separately. $\endgroup$ – Shamina Jan 17 '17 at 8:52
2
$\begingroup$

Perhaps,

ClearAll[insertF]
insertF[mat_, diag_, offdiag_, d_] := ArrayFlatten@ReplacePart[mat, {{r_, c_} :>
  Which[r == c, {{r, 0}, {0, 0}} + diag, Or[c == r + d, r == c + d], offdiag, True, 0]}]

insertF[MLarge, {{0, x}, {y, 0}}, {{0, j}, {k, 0}}, 2] // MatrixForm

Mathematica graphics

$\endgroup$
  • $\begingroup$ Thanks for your attention and time. Can you explain a little bit about the code? It will be very nice of you. So that I can use this, to say for two or more matrices(with function) addition and at positions defined by the conditions(This what I mean general rule at the end of question, I am sorry for trouble, but it will complete the problem, as I have not posted the actual problem due to ongoing research on it, only an alias). I would like to do them by my own. $\endgroup$ – Shamina Jan 17 '17 at 8:58
  • $\begingroup$ I hope it will also be helpful to others. $\endgroup$ – Shamina Jan 17 '17 at 18:02

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.