【问题标题】:How replicate eigen::matrix dynamically如何动态复制 eigen::matrix
【发布时间】:2021-12-19 23:45:27
【问题描述】:

在计算两个特征图之间的距离矩阵时。

A:(M,1)
B:(N,1)

我想重复 B 列以等于 A 行。

在 NumPy 中很简单:

A = np.random,rand(100, 1)
B = np.random.rand(88, 1)
np.repeat(B, A.shape[0], axis=1)

但在 c++ Eigen 中,不适用于动态分配重复形状。

MatrixXi A = MatrixXi::Random(100,1);
MatrixXi B = MatrixXi::Random(88,1);

B.replicate<1, A.rows()>(); // This will cause failure

【问题讨论】:

    标签: c++ eigen


    【解决方案1】:

    在 C++ 中实现与 python 的 numpy 版本相同效果的正确方法是:

    B.replicate<1, 100>(); 
    

    上面将根据需要进行复制。
    或者你可以使用:

    B.replicate(1, A.rows());
    

    【讨论】:

    • 这不是我想要的,A行和B行是未知的。
    • @ZhengfangXin 为此,您可以改用 B.replicate(1, A.rows()); 。它们都使用相同的函数模板replicate&lt;&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多