Home»Documentation»Data Analysis Library User's Guide»Grouping and Aggregation»Aggregating vectors and matrices

## Aggregating vectors and matrices | Extreme Optimization Numerical Libraries for .NET Professional |

The same methods that are available for aggregating over the columns of data frames are also available for vectors and matrices.

For matrices, it is possible to aggregate over both the rows and columns. The AggregateRows method aggregates the rows of a matrix into a single vector or, when multiple aggregators have been supplied, a matrix. The AggregateRowsBy method aggregates groups within each row. In the following example, we create a matrix of random numbers and compute the means of its rows. We then create a moving window of length 5, and construct a matrix that contains the moving average of each row:

var m1 = Matrix.CreateRandom(10, 100); var rowMeans = m1.AggregateRows(Aggregators.Mean); var window = Grouping.Window(m1.ColumnCount, 5); var rowMAs = m1.AggregateRowsBy(window, Aggregators.Mean);

The AggregateColumns method aggregates the columns of a matrix into a single vector or, when multiple aggregators have been supplied, a matrix. The AggregateColumnsBy method aggregates groups within each column. The same example as above, but aggregating over columns instead of rows, becomes:

var m2 = Matrix.CreateRandom(100, 10); var columnMeans = m2.AggregateColumns(Aggregators.Mean); window = Grouping.Window(m2.RowCount, 5); var columnMAs = m2.AggregateColumnsBy(window, Aggregators.Mean);

There is one more method for aggregating over columns:
AggregateColumnsListwise

var m2 = Matrix.CreateRandom(100, 10); var columnMeans = m2.AggregateColumns(Aggregators.Mean); window = Grouping.Window(m2.RowCount, 5); var columnMAs = m2.AggregateColumnsBy(window, Aggregators.Mean);

Similarly, vectors have Aggregate and AggregateBy methods that aggregate over the entire vector or each group, respectively. In the next example, we create a vector of random values and compute its kurtosis. We then compute the kurtosis of each chunk of length 10:

var v = Vector.CreateRandom(100); var K = v.Aggregate(Aggregators.Kurtosis); var partition = Grouping.Partition(v.Length, 10); var Ks = v.AggregateBy(partition, Aggregators.Kurtosis);

The AggregateColumns method aggregates the columns of a matrix into a single vector or, when multiple aggregators have been supplied, a matrix. The AggregateColumnsBy method aggregates groups within each column. The same example as above, but aggregating over columns instead of rows, becomes:

var m2 = Matrix.CreateRandom(100, 10); var columnMeans = m2.AggregateColumns(Aggregators.Mean); window = Grouping.Window(m2.RowCount, 5); var columnMAs = m2.AggregateColumnsBy(window, Aggregators.Mean);

There is one more method for aggregating over columns:
AggregateColumnsListwise

Copyright Â© 2004-2023,
Extreme Optimization. All rights reserved.

*Extreme Optimization,* *Complexity made simple*, *M#*, and *M
Sharp* are trademarks of ExoAnalytics Inc.

*Microsoft*, *Visual C#, Visual Basic, Visual Studio*, *Visual
Studio.NET*, and the *Optimized for Visual Studio* logo

are
registered trademarks of Microsoft Corporation.