Language Reference


COL Function

COL (x);

The COL function is part of the IMLMLIB library . The COL function returns a matrix that has the same dimensions as the x matrix and whose jth column has the value j. You can use the COL and ROW function to extract elements of a matrix. For example, the following statements fill the subdiagonal, superdiagonal, and main diagonal of a matrix with a sequence of numbers:

x = j(5, 5, 0);             /* allocate 5 x 5 matrix of zeros */
r = row(x);                 /* create helper matrices */
c = col(x);
idx = loc(abs(r-c)<= 1);    /* indices of sub-, super-, and main diagonal */
x[idx] = 1:ncol(idx);       /* fill with 1,2,3,... */
print x[format=Best3.];

Figure 25.73: A Tridiagonal Matrix

x
1 2 0 0 0
3 4 5 0 0
0 6 7 8 0
0 0 9 10 11
0 0 0 12 13



If r = row(m) and c = col(m) are two matrices, then you can use logical comparisons of r and c to describe certain submatrices, such as in Table 25.1:

Table 25.1: Some Common Submatrices

Submatrix

Index by LOC of

Diagonal

r = c

Upper triangular

r < c

Lower triangular

r > c

Banded with radius d

abs(r-c) <= d

Antidiagonal

r + c -1 = ncol(r)


You can also use the COL function to generate an ID variable when you convert data from a wide format to a long format. For example, the following statements show how to generate a column vector with values $\{ 1, 2, 3, 1, 2, 3, \ldots , 1, 2, 3\} $:

NumSubjects = 5;        /* number of subjects */
NumRepeated = 3;        /* number of repeated obs per subject */
Y = col(j(NumSubjects, NumRepeated));
Repl = shape(Y, 0, 1);  /* {1, 2, 3, 1, 2, 3, ..., 1, 2, 3}   */