問題
您在矩陣中有數據並需要數據表,或者反過來
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 ];零下標表示“所有這個維度”。 矩陣索引從一開始。
謝謝,非常有用,有什麼方便的方法可以從數據表中獲取行向量嗎?
謝謝!