我有 20 列數據。我需要將每一列與所有其他列相乘 - 一個接一個。例如,第 2 列 * 第 3 列;第 2 列 * 第 4 列;第 2 列*第 5 列...第 3 列*第 4 列、第 3 列*第 5 列...是否有使用 JMP 的簡單方法?
感謝您的幫助,
傑斯
您可以使用 nchoosekmatrix() 獲取列的組合。如果您只想要數字矩陣(無論出於何種原因),您可以很快完成。
Names Default to Here( 1 );
dt = new table("Test",
add rows(20),
New Column("C", <<SET each="" value=""> 14,
dt << Delete Column(15::ncols(dt));
);
cols_to_multiply = 2::14;
n = nitems(cols_to_multiply);
nck_mat = nchoosekmatrix(n, 2); // this only picks 2 of each combination
// if you just want a matrix
st = HPTime();
dt_mat = dt[0, cols_to_multiply]; //get the values as a matrix
big_mat = dt_mat[0, nck_mat]; // get the combinations of dt values
m_mat = shape(big_mat, nitems(big_mat)/2); // have to shape this to only 2 columns so you can multiply accross
m_mat = shape(m_mat[0, 1] :*</img> m_mat[0, 2], 20); // multiply together then reshape to n rows
tot_mat = HPTime() - st;
//turning it into a data table
st = HPTIme();
nck_cols = shape(cols_to_multiply[nck_mat], nrows(nck_mat), 2);// to get the column names
for(i=1, i<=ncols(m_mat), i++,
col_name = (Column(dt, nck_cols[i, 1]) << Get Name()) || " * " || (Column(dt, nck_cols[i, 2])<< Get name());
New Column(col_name, <<SET values=""></SET></SET>你好@傑斯,
我不知道有任何可交互使用的那種成對乘法,但它可以通過一些簡單的腳本來完成。 你用過 JSL 嗎? 下面是一種允許您指定開始和結束的列號的方法,腳本負責製作成對的乘積列。 如果您之前沒有使用過 JSL,請告訴我,我很樂意詳細介紹一下。
dt = Open( "$SAMPLE_DATA\US Demographics.jmp" );
startCol = 5;
endCol = 14;
For( i = startCol, i <= endCol - 1, i++,
For( j = i + 1, j <= endCol, j++,
colName = Eval( :Column( i ) << Get Name ) || " * " || Eval( :Column( j ) << Get Name );
New Column( colName, seteachvalue( :Column( i )[] * :Column( j )[] ) );
)
);
我希望這有幫助!
@朱利安