submat

Purpose

Extracts a submatrix of a matrix, with the appropriate rows and columns given by the elements of vectors.

Format

y = submat(x, r, c)
Parameters:
  • x (NxK matrix) – data
  • r (LxM matrix) – row indices
  • c (PxQ matrix) – column indices
Returns:

y ((L*M)x(P*Q) matrix) – submatrix of x, y may be larger than x.

Examples

// Create 12x1 vector with consecutive numbers
x = seqa(1, 1, 12);

// Reshape the 12x1 vector into a 3x4 matrix
x = reshape(x, 3, 4);

v1 = { 1 3 };
v2 = { 2 4 };

// Extract sub-matrices
y = submat(x, v1, v2);
z = submat(x, 0, v2);

After the code above, the matrix values are:

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

y =  2  4
    10 12

     2  4
z =  6  8
    10 12

Remarks

If \(r = 0\), then all rows of x will be used. If \(c = 0\), then all columns of x will be used.

See also

Functions diag(), vec(), reshape()