byColumn

Syntax

byColumn(func, X)

Arguments

func is a unary function. When function with multiple parameters is specified for func, partial application is used to fix part of the parameters. It can be a vector function (where the input vector and output vector are of equal length) or an aggregate function.

X is a matrix. Each column of X is an argument to the func.

Details

Applies the specified function to each column of the matrix X.

Examples

When func is a unary function that does not support matrix operations, the byColumn function is equivalent to each.

$ def myvfunc(x): var(x).log()
$ m = matrix(1.1 2.3 2.1 3.5 4.2, 3.3 2.5 4.2 5.1 0, -1 3.3 2 1.7 2.3)
$ byColumn(myvfunc, m)
[0.3974329364109,1.334211281249665,0.945072533299607]

To specify a function with multiple parameters for func:

$ byColumn(add{2}, m)
col1 col2 col3
3.1  5.3  1
4.3  4.5  5.3
4.1  6.2  4
5.5  7.1  3.7
6.2  2    4.3

$ byColumn(add{1 2 3 4 5}, m)
col1 col2 col3
2.1  4.3  0
4.3  4.5  5.3
5.1  7.2  5
7.5  9.1  5.7
9.2  5    7.3

When func is a user-defined function:

$ def my_func(x){
$    return iif(x > 0, 1, -1)
$ }
$ m = matrix(3 -6 5 0, 2 -9 -4 5)
$ byColumn(my_func, m)
col1 col2
1    1
-1   -1
1    -1
-1    1

When func is a nested function:

$ m = matrix(1 5 3 , 7 5 2)
$ byColumn(accumulate{def (x, y):iif(x > 5, y-1, y+1), ,1}, m)
col1 col2
2    8
6    4
2    3

Related functions:byRow