在matlab中,我们写道:
H2= H(p==1,:)其中H2和H是稀疏双矩阵,p是逻辑向量。
我如何用itpp编写它
发布于 2015-02-02 17:41:03
不是100%熟悉itpp,但我会尝试如下所示
int h = 0, w = H.cols();
// count number of set elements in p to get number of rows of H2
for ( int i = 0 ; i < p.length() ; i++ ) {
h += (p[i] == 1);
}
// alocate H2
H2 = Sparse_Mat( h, w, H.nnz() ); // estimate number of nonzeros in H2
// copy the relevant elements
for ( int i = 0, i2 = 0 ; i < p.length() && i2 < h ; i++ ) {
if ( p[i] != 1 ) {
continue;
}
H2.set_submatrix( i2, 0, H.get_submatrix( i, i+1, 0, w ).full() );
i2++;
}显然,使用get_col和set_col处理稀疏列要容易得多,因此可以考虑先转置H,然后再执行返回H2.transpose()的操作。
https://stackoverflow.com/questions/28273362
复制相似问题