Compares labels at corresponding positions (element-wise) between two equal-length vectors `a` and `b`, with explicit NA handling. Creates a sparse matrix with entries only on the diagonal.
Arguments
- a
The first categorical label vector.
- b
The second categorical label vector. Must have same length as `a`.
- type
The type of comparison: "s" for same labels (a[i] == b[i]) or "d" for different labels (a[i] != b[i]). Default is "s".
- return_matrix
A logical flag indicating whether to return the result as a sparse matrix (default: TRUE) or a triplet matrix with columns (i, j, x).
- dim1
The row dimension of the output matrix (default: length(a)).
- dim2
The column dimension of the output matrix (default: length(b)).
Value
If return_matrix is TRUE, a sparse diagonal matrix where entry (i, i) is 1 if the labels at position i satisfy the comparison (and neither is NA). If return_matrix is FALSE, a 3-column matrix of (row, col, value) triplets.
Details
This function performs element-wise (positional) comparison, NOT all-pairs comparison. Positions where either label is NA are excluded from the result.
For all-pairs comparison (block structure), use binary_label_matrix.
For diagonal comparison without NA handling, use diagonal_label_matrix.