1
$\begingroup$

I have a row matrix

f[m_] = Table[f[m, n], {n, 1, 6}]

I want to use it to define another matrix g[m_, i_, j_] such that

g[m_, i_, j_] = f[m]

with jth element is substituted with the summation of jth & ith elements while ith elements turns to be zero.

$\endgroup$
  • 2
    $\begingroup$ I think I understood your question, but it's useful to include a sample input with the desired output in your post. $\endgroup$ – march Jul 25 '17 at 17:24
1
$\begingroup$

Like this?

f[m_] = Table[f[m, n], {n, 1, 6}];
g[m_, i_, j_] := ReplacePart[f[m], {j -> f[m][[j]] + f[m][[i]], i -> 0}]

Similarly:

g[m_, i_, j_] := Normal@SparseArray[{j -> f[m][[j]] + f[m][[i]], i -> 0, k_ :> f[m][[k]]}, Length@f[m]]

Or:

g[m_, i_, j_] := SparseArray[{{j, i} -> 1, {i, i} -> 0, {k_, k_} -> 1}, {#, #} &@Length@f[m]].f[m]

Then,

g[m, 1, 3]
(* {0, f[m, 2], f[m, 1] + f[m, 3], f[m, 4], f[m, 5], f[m, 6]} *)
$\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.