0

I am trying to create a vector from another submatrix or subvector. I have tried following but get this error error: no match for ‘operator=’

 int m_size = 10; 
 boost::numeric::ublas::matrix<double> u_a(m_size, m_size);
  boost::numeric::ublas::vector<double> u_v(m_size);

    for (int i = 0; i < m_size; i = i + 1 ){
        for (int j = 0; j < m_size; j++ ){
         double rand1 = (rand()%10+1) +  ((double) rand() / (RAND_MAX));
         u_a(i,j) = rand1;
    }
    double rand3 = (rand()%10+1) +  ((double) rand() / (RAND_MAX));
    u_v(i) = rand3;
    }

for (int i = 0; i < m_size; i = i + 1 ){
  boost::numeric::ublas::matrix<double> u_p(i, i);
  boost::numeric::ublas::vector<double> u_v2(i);
  u_p = subrange(u_a, 0, i, 0, i); 
  // I have tried following two   
  //u_v2 = subrange(u_a, 0,1,0,5);
  //u_v2 = subrange(u_v, 1,i); 
 }
asdfkjasdfjk
  • 3,784
  • 18
  • 64
  • 104
  • Can you post an [MCVE](http://stackoverflow.com/help/mcve) and specify compiler and Boost version? Your code works for me [on CoLiRu](http://coliru.stacked-crooked.com/a/0b086dbf82dcf2f9) with Boost 1.59. – rhashimoto Jun 15 '16 at 16:48
  • You will have to uncomment one of the last two lines, I am on linux, gcc 4.8 and boost 1.54 – asdfkjasdfjk Jun 15 '16 at 17:08

1 Answers1

1

The issue with this line:

u_v2 = subrange(u_a, 0,1,0,5);

is that the subrange of a matrix is a matrix expression, not a vector expression, and you are assigning to a vector. You can use the row() free function to perform the desired conversion:

u_v2 = row(subrange(u_a, 0, 1, 0, 5), 0);

This line:

u_v2 = subrange(u_v, 1,i); 

compiles fine, but fails with a runtime error. This is because on the initial iteration, the requested range goes from 1 to 0 which is invalid. If you wanted the subvector of i - 1 elements starting at index 1, you would do it like this:

u_v2 = subrange(u_v, 1, 1 + i); 

Live at CoLiRu

rhashimoto
  • 15,650
  • 2
  • 52
  • 80
  • For `'u_v2 = subrange(u_v, 1, 1 + i);` I still get error `error: no matching function for call to` but first solution is working great. – asdfkjasdfjk Jun 15 '16 at 19:02
  • The earliest versions I have are g++ 4.9.2 and Boost 1.55, and it works. The [version history](http://www.boost.org/users/history/) for Boost doesn't show any updates to uBLAS for 1.55. The docs for 1.54 do include the [`subrange()` free function](http://www.boost.org/doc/libs/1_54_0/libs/numeric/ublas/doc/vector_proxy.htm#11SimpleProjections) for vector ranges. Could be a compiler bug, I guess. – rhashimoto Jun 15 '16 at 19:13
  • Are you using the verbatim code from [my CoLiRu](http://coliru.stacked-crooked.com/a/c31097f83085f349)? – rhashimoto Jun 15 '16 at 19:17