Change the Elements of a Matrix

Any of the indexing expression can be used to modify part of the elements of a matrix. For example, A[2, 3]=-1 would change the element at row 2 and column 3 to $ -1$.
>> A = zeros(3,5)
   0   0   0   0   0
   0   0   0   0   0
>> A[2,3]=2.3
   0   0    0    0   0
   0   0  2.3    0   0
>> A[:, 1] = [5; 10]
    5   0    0    0   0
   10   0  2.3    0   0
>> A[:, 3] = A[:, 3}  + A[:, 1]
    5   0     5    0   0
   10   0  12.3    0   0
>> A[2, :] = 9
    5   0     5    0   0
    9   9     9    9   9
Usually when an indexing expression is used as an lvalue, the dimension and size of the right hand side of the assignment should match that of the indexing expression to make the assignment possible. The only exception is when the right hand side is a scalar, then all the indexed elements of the matrix will be set to the same value.
>> A = zeros(3,3)
   0   0   0
   0   0   0
   0   0   0
>> A[\] = 1;
   1   0   0
   0   1   0
   0   0   1
When updating the contents of a matrix, the indices don't have to be within the upper bounds, which makes it possible to make the size of the matrix grow. As the size of a matrix is being extended, the new entries are set to zero, except for those being specified by the assignment statement. For example,
>> X = [1,4,9]
   1   4   9
>> X[4] = 16
   1   4   9   16
>> x[8]=36
   1   4   9    16    0    0   0   36



oz 2009-12-22