这困扰了我几个小时,所以也许有人可以帮忙。我正在尝试使用uBLAS将以下简单的Matlab程序转换为C++:

>> R = eye(4);
>> R(:,3) = R(:,4);
>> R

R =

     1     0     0     0
     0     1     0     0
     0     0     0     0
     0     0     1     1

这是我的尝试,但是没有用:
#include <iostream>

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>

using namespace boost::numeric::ublas;

int main ()
{
    matrix<double> Idmx = identity_matrix<double> (4);
    project (Idmx, range (0, 4), range (2, 3)) = project (Idmx, range (0, 4), range (3, 4));

    std::cout << Idmx << std::endl;

    int temp;
    std::cin >> temp;
}

产生的输出是:
[4,4]((1,0,0,0),(0,1,0,0),(0,0,0,0),(0,0,1,1))

我不明白为什么第三行现在全为零。有人可以指出我的解决方案吗?

谢谢!

最佳答案

一个例子:

的MATLAB

>> m = [0 1 2 3 4; 5 6 7 8 9; 10 11 12 13 14; 15 16 17 18 19]
m =
     0     1     2     3     4
     5     6     7     8     9
    10    11    12    13    14
    15    16    17    18    19
>> m(:,3) = m(:,4)
m =
     0     1     3     3     4
     5     6     8     8     9
    10    11    13    13    14
    15    16    18    18    19

C++ / uBLAS
#include <iostream>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/io.hpp>

namespace bnu = boost::numeric::ublas;

int main()
{
    /* create and fill matrix */
    bnu::matrix<double> m(4,5);
    for (unsigned i = 0; i < m.size1(); ++i)
        for (unsigned j = 0; j < m.size2(); ++j)
            m(i,j) = m.size2()*i + j;

    /* All the following are equivalent */

    // indexing (i,j)
    for(unsigned i = 0; i < m.size1(); ++i)
        m(i,2) = m(i,3);

    // column
    bnu::column(m,2) = bnu::column(m,3);

    // project+range
    bnu::project(m, bnu::range(0,m.size1()), bnu::range(2,3)) = bnu::project(m, bnu::range(0,m.size1()), bnu::range(3,4));

    /* print matrix */
    std::cout << m << std::endl;

    return 0;
}

输出:
[4,5]((0,1,3,3,4),(5,6,8,8,9),(10,11,13,13,14),(15,16,18,18,19))

09-07 07:09