MATLAB: Can I change an individual matrix element by a given value in one line

MATLAB: Can I change an individual matrix element by a given value in one line

MATLABmatrixmatrix arraymatrix manipulation

I want to increase one particular element in an array by a specified amount. Effectively, the code I want is:
a = [1, 2, 3];
a(1) += 2;
In my head, this gives an output of
a =
[3, 2, 3]
However doing this gives an error, saying the array element is “not a valid target for an assignment”.
Is there a way of doing this in one line, or do I have to do something like:
b = a(1) + 2;
a(1) = b;

Best Answer

  • a(1) = a(1) + 2;
  • Add a Comment

    Your email address will not be published. Required fields are marked *