byRow

Syntax

byRow(func, X, [Y])

Or

func:H(X)

Or

func:H(X, [Y])

Arguments

func is either a vector function (both input and output are vectors of equal length) or an aggregate function.

X is a matrix/table/tuple/array vector/columnar tuple.

Y is a matrix/table/tuple/array vector/columnar tuple.

For the arguments and calculation rules of other row-based functions, refer to Row-Based Functions.

Details

If func is a unary function, apply the specified function to each row of X; if func is a binary function, apply func(Xi, Yi) to each row of X and Y.

Note: Since version 2.00.11, byRow can achieve the roles of row-based functions with equivalent performance and can be applied to all row-based scenarios. For example, byRow(sum, X) is equivalent to rowSum(X).

Calculation rules:

  • If X/Y is a matrix, table, or tuple, byRow applies func to each column of the transpose of X/Y.

    • If func is a vector function, byRow returns the transpose of the result of func.

    • If func is an aggregate function, byRow directly returns a vector. Certain aggregate functions in DolphinDB are optimized to work natively by row, requiring no transpose of the input X/Y. These include: sum, sum2, avg, min, max, count, imax, imin, imaxLast, iminLast, prod, std, stdp, var, varp, skew, kurtosis, any, all, corr, covar, wavg, wsum, beta, euclidean, dot, tanimoto.

  • If X/Y is an array vector or columnar tuple, byRow applies func to each row of X/Y.

Return Value:

  • If func is an aggregate function, byRow returns a vector of the same size as the number of rows in X/Y.

  • If func is a vector function, byRow returns a result with the same form and dimension as X/Y.

Examples

X/Y is a matrix.

m=matrix([1 3 4 2,1 2 2 1])
max:H(m)
// output
[1,3,4,2]

cummax:H(m) 
// output
col1	col2
1	1
3	3
4	4
2	2

n=matrix([11 5 9 2,8 5 3 2])
corr:H(m,n)
// output
[,,1,]

X/Y is a table.

qty1 = 2200 1900 2100 3200 6800 5400 1300 2500 8800
qty2 = 2100 1800  6800 5400 1300 2400 8500 4100 3200
t = table(qty1, qty2);
max:H(t) 
// output
[2200,1900,6800,5400,6800,5400,8500,4100,8800]

cummax:H(t) 
// output
qty1	qty2
2,200	2,200
1,900	1,900
2,100	6,800
3,200	5,400
6,800	6,800
5,400	5,400
1,300	8,500
2,500	4,100
8,800	8,800

qty3 = 7800 5400 5300 2500 1800 2200 3900 3100 1200
qty4 = 3200 2800 6400 8300 2300 3800 2900 1600 2900
t1 = table(qty3, qty4);
corr:H(t,t1)   
// output
[1,1,1,1,-1,-1,-1,-1,-1]

X/Y is a tuple.

tp=[1 3 4 2,1 2 2 1]
sum:H(tp)  
// output
[2,5,6,3] 

cummax:H(tp) 
// output
([1,3,4,2],[1,3,4,2]) 

tp1=[11 23 14 21,10 12 32 21]
corr:H(tp,tp1) 
// output 
[,1,-1,]

X/Y is an array vector.

a=array(INT[], 0, 10).append!([1 2 3, 4 5 4, 6 7 8, 1 9 10]);
sum:H(a) 
// output
[6,13,21,20] 

cummax:H(a)
// output
[[1,2,3],[4,5,5],[6,7,8],[1,9,10]]

b=array(DOUBLE[], 0, 10).append!([11.8 21.2 23.9, 83.3 90.2 78.2, 86.5 52 36.5, 10.1 12.4 16.8])
corr:H(a,b)  
// output
[0.95,0.90,-0.97,0.82]

X/Y is a columnar tuple.

ctp=[1 3 4 2,1 2 2 1]
ctp.setColumnarTuple!()
sum:H(ctp)  
// output
[10,6]

cummax:H(ctp)
// output
([1,3,4,4],[1,2,2,2])

ctp1=[11 23 14 21,10 12 32 21]
ctp1.setColumnarTuple!()

corr:H(ctp,ctp1)
// output
[0.25, 0.37]
m=matrix(1 1, 2 3, 2 1);
m;
col1 col2 col3
1 2 2
1 3 1
byRow(add{10 20 30},m);
col1 col2 col3
11 22 32
11 23 31
byRow(mode,m);
// output
[2,1]
b=array(DOUBLE[], 0, 10).append!([11.8 21.2 23.9, 83.3 90.2 78.2 86.5, 10.1 12.4 16.8])
byRow(add{100},b)
// output
[111.8 121.2 123.9,183.3 190.2 178.2 86.5,110.1 112.4 116.8]

byRow(imax,b)
// output
[2,1,2]

Related function: byColumn