2
$\begingroup$

Consider matrix

mat[x_] := {{1, 2}, {3, x}}

with

mat[x][[1]]

giving

{1, 2}

I wanna do

mat[x][[1]] = 0;

but get error

"mat[x] in the part assignment is not a symbol"

How to do operation without error?

I wanna get

{{0, 0}, {3, x}}
$\endgroup$
  • 1
    $\begingroup$ What matrix do you hope to have after the operation? $\endgroup$ – Marius Ladegård Meyer Nov 2 '17 at 12:57
  • $\begingroup$ @Marius I hope have {{0, 0}, {3, x}} $\endgroup$ – Nigel1 Nov 2 '17 at 13:00
  • 1
    $\begingroup$ mat[x][[1,1]] is the first element of 1st row, mat[x][[1,2]] the 2nd of 1st row, mat[x][[2,1]] etc... but you have defined your function, so why don't you define a new one? $\endgroup$ – Tom Zinger Nov 2 '17 at 13:02
  • 4
    $\begingroup$ ReplacePart[mat[x], 1 -> {0, 0}] $\endgroup$ – Bob Hanlon Nov 2 '17 at 13:06
  • 2
    $\begingroup$ To extend @Bob's solution: DownValues[mat] = Fold[MapAt[#2, #1, {1, 2}] &, DownValues[mat], {ReplacePart[#, 1 -> {0, 0}] &, Evaluate}]; $\endgroup$ – J. M. will be back soon Nov 2 '17 at 14:00

Your Answer

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

Browse other questions tagged or ask your own question.