特征索引,更新特定行的所有列

时间:2018-12-21 00:30:30

标签: c++ eigen

假设我有一个class UserInterface(Frame): # Launch the df in a pandastable frame def __init__(self, parent=None): global ui_df ui_df = pos_df self.parent = parent self.refresh_df(df = ui_df) def refresh_df(self, df): Frame.__init__(self) self.main = self.master f = Frame(self.main) f.grid(column=0, row=1, sticky=(E, W)) screen_width = f.winfo_screenwidth() * 0.8 screen_height = f.winfo_screenheight() * 0.7 self.table = pt = Table(f, dataframe=df, height = screen_height, width = screen_width) pt.show() return def change_df(self, col_val_input): #Responds to button ui_df['Test col'] = col_val_input self.refresh_df(df=ui_df) def change_df_combo(self, event): #Responds to combobox, supposed to filter by 'Sec_type' combo_selection = str(combo_box.get()) ui_df = pos_df[pos_df['Sec_type'] == combo_selection] ui_df['Test col combo'] = combo_selection self.refresh_df(df=ui_df) (或ArrayXXfMatrixXf。在for循环的每次迭代中,我都想用m逐行填充m

VectorXf

如何在Eigen中实现目标?

2 个答案:

答案 0 :(得分:2)

为简化@Kunal的答案,您可以直接修改数组(或矩阵)的行(或列),而无需创建临时向量。在您的示例中,您可以使用.setLinSpaced()

Eigen::ArrayXXf m(5, 5);

for (int i = 0; i < 5; i++) {
    m.row(i).setLinSpaced(i,i+4); //.col(i) would be slightly more efficient
}

或使用逗号初始化程序:

for (int i = 0; i < 5; i++) {
    m.row(i) << i, i+1, i+2, i+3, i+4;
}

答案 1 :(得分:1)

使用block()函数。

#include <iostream>
#include <Eigen/Dense>

using namespace std;

int main()
{
    Eigen::ArrayXXf m(5, 5);

    for (int i = 0; i < 5; i++) {
        Eigen::VectorXf vec(5);
        vec << i, i + 1, i + 2, i+3, i+4;

        m.block(i, 0, 1, 5) << vec.transpose();
    }

    std::cout << m << std::endl;
    return 0;
}

输出:

0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8

编辑:

还有一种更简单的选择:row()函数。

#include <iostream>
#include <Eigen/Dense>

using namespace std;

int main()
{
    Eigen::ArrayXXf m(5, 5);

    for (int i = 0; i < 5; i++) {
        Eigen::VectorXf vec(5);
        vec << i, i + 1, i + 2, i+3, i+4;

        m.row(i) = vec.transpose();
    }

    std::cout << m << std::endl;
    return 0;
}

输出:

0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8

P.S。 transpose()是必需的,因为默认情况下Eigen :: VectorXf是列向量,不是行向量。