开发者

Newbie question regarding submatrix manipulation using BOOST uBLAS

This has been puzzling me for a few hours, so maybe someone here can help. I am trying to translate the following simple Matlab program into C++ using uBLAS:

>> 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

This is my attempt, yet it is not working:

#include <iostream>

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ubl开发者_运维问答as/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;
}

The output produced is:

[4,4]((1,0,0,0),(0,1,0,0),(0,0,0,0),(0,0,1,1))

I don't understand why the third row is now all zeroes. Can someone point me to a solution?

Thank you!


An example:

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;
}

The output:

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


Both results are exactly the same. In both cases you are copying the fourth column into the third one. Since the fourth column is [0; 0; 0; 1], the third column will be also [0; 0; 0; 1] after that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜