Index expression as lvalue

The lvalue is the expression of the left-hand side of an assignment expression. All the index expressions we have just described can be used as lvalues for modifying the contents of matrix. For examples
>> A = zeros(5,5);
>> A[2,3]=2.3
>> A[1:3, 1:3] = rand(3,3)
>> A[$]=100

Usually when an index expression is used as an lvalue, the dimension and size of the right hand side of the assignment should match that of the index 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 scalar value.

>> A = zeros(3,3)
   0   0   0
   0   0   0
   0   0   0
>> A[1, :] = 3
   3   3   3
   0   1   0
   0   0   1
>> A[\] = 1
   1   3   3
   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 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    0    0    0   0   36



oz 2009-12-22