问题
您在矩阵中有数据并且需要数据表,或者反之亦然
Data table or matrix?Data table or matrix?数据表还是矩阵?
解决方案
有一个函数 AsTable(),可以从矩阵创建一个新的数据表,还有一个数据表方法 GetAsMatrix(),可以从表中的数字列创建一个矩阵。
还有一种方法可以直接使用索引数据表数据表下标。
// make a matrix of 5 rows, 3 cols, random values
mat = J( 5, 3, Random Normal() );
// make a data table from a matrix
dt = As Table( mat );
// make a matrix from a data table
mat2 = dt << GetAsMatrix;
// comparing two matrices returns a matrix of answers
// and the All function returns true if all of the
// matrix values are true
Show( All( mat == mat2 ) );
// use data table subscripting to get a matrix of all
// rows and all columns from the table; the zero subscript
// is used with matrix and data table to mean "all
// rows or all columns"
mat3 = dt[0, 0];
Show( All( mat == mat3 ) );
谢谢!
数据表下标描述将数据表索引为矩阵,以及JSL 矩阵是什么?展示如何使用矩阵下标。 因此,使用数据表(或矩阵),您可以从任一数据表中获取一行,如下所示:
// if dt is a table...
rowVector = dt[ rowNumber, 0 ]; // all columns
rowVector = dt[ rowNumber, { age, height, weight } ]; // selected columns
// if m is a matrix
rowVector = m[ row, 0 ];
rowVector = m[ row, 5::9 ]; // selected items from row
// and both dimensions at once
patch = m[ 3::5, 7::11 ];零下标表示“所有这个维度”。 矩阵索引从一开始。
谢谢,非常有用,有什么方便的方法从数据表中获取行向量吗?
谢谢!