# Part Assignment of variables with argument

I do not really understand why this:

a = {1, 2, 3};
a[[1]] = 3;
a


{3,2,3}

works as desired, but this does not:

b[1] = {1, 2, 3};
b[1][[1]] = 3;
b[1]


Set::setps: b[1] in the part assignment is not a symbol.

Can I not treat variables with arguments like 'normal' variables?

• closely related: 148387 – Kuba Oct 22 '19 at 9:20

You can use ReplacePart:

ClearAll[b]
b[1] = {1, 2, 3};
b[1] = ReplacePart[ b[1], 1 -> 3];
b[1]


{3, 2, 3}

Alternatively, MapAt:

ClearAll[b]
b[1] = {1, 2, 3};
b[1] = MapAt[3 &, b[1], {1}];
b[1]


{3, 2, 3}

• Interesting thank you. What would I do If I wanted to set a column in a two Dimensional array? Like this: a = ConstantArray[1, {10, 10}] a[[;; , 3]] = ConstantArray[2, 10]; a – Mr Puh Oct 22 '19 at 9:33
• @MrPuh, the code in your comment works as expected. – kglr Oct 22 '19 at 9:35
• yeah, I mean in the sense of the question, I would want something like this now: a[1] = ConstantArray[1, {10, 10}] a[1][[;; , 3]] = ConstantArray[2, 10]; a[1] – Mr Puh Oct 22 '19 at 9:43
• @MrPuh, you can do ClearAll[a];a[1] = ConstantArray[1, {10, 10}] ;a[1]=MapAt[ 2&,a[1], {All,3}];a[1] or ClearAll[a];a[1] = ConstantArray[1, {10, 10}] ; a[1]=ReplacePart[ a[1], {_,3}->2];a[1] – kglr Oct 22 '19 at 9:53
• ... you can also use SubsetMap: ClearAll[a];a[1] = ConstantArray[1, {10, 10}] ; a[1]=SubsetMap[ConstantArray[2,10]&,a[1], {All,3}];a[1] – kglr Oct 22 '19 at 9:55