Using backslash \ to reference diagonals

If is a square matrix, then

In the expression A[\,k], k can be a vector as well. For example, if A is , A[\, 1:n-1] would return the elements of the upper triangular part of A (as a vector).

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9]  

   1   2   3
   4   5   6
   7   8   9

>> A[\, 1:2]
   2
   6
   3

>> A[1,1:2] = 0

   1   0   0
   4   5   0
   7   8   9
If A and B are two matrices, A[\, 1:9]=B[\,1:9] would copy the upper triangle of B to A.



oz 2009-12-22