我有 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 )[] ) );
)
);
我希望这有帮助!
@朱利安