# fold: Folding square matrices around the diagonal In netseg: Measures of Network Segregation and Homophily

 fold R Documentation

## Folding square matrices around the diagonal

### Description

Fold a square matrix by collapsing lower triangle on upper triangle, or vice versa, through addition.

### Usage

``````fold(x, direction = c("upper", "lower"))
``````

### Arguments

 `x` square numeric matrix `direction` character, one of `"upper"` or `"lower"`, direction of folding

### Details

By default, for `direction=="upper"`, the function takes the values in the lower triangle of `x` and adds them symetrically to the values in the upper triangle. The values on the diagonal remain unchanged. The lower triangle is filled with 0s. If `direction=="lower"` the upper triangle is collapsed on to the lower triangle.

### Value

Square matrix of the same `dim` as `x` with the lower (upper) triangle folded onto the upper (lower) triangle.

`upper.tri()`, `lower.tri()`, `symmetrize()`

### Examples

``````(m <- matrix(1:4, 2, 2))
(f1 <- fold(m))
(f2 <- fold(m, "lower"))

stopifnot( all.equal(diag(m), diag(f1)) )
stopifnot( all.equal(diag(m), diag(f2)) )
stopifnot( all.equal(f1[1,2], m[2,1] + m[1,2]) )
stopifnot( all.equal(f2[2,1], m[2,1] + m[1,2]) )
``````

netseg documentation built on July 9, 2023, 6:33 p.m.