Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

transposing a 2Dvector/matrix

Tags:

c++

I have the following 2d vector/matrix X and a vector Y as below:

std::vector<double> Y; 
unsigned int ctr=2;
std::vector<std::vector<double> >X(ctr,Y);

I now want to create a transpose of X,i.e. Xtrans, so am declaring it as below

std::vector<std::vector<double> >Xtrans(Y,ctr);

but it gives me the following compilation error:

test.cpp:128:58: error: no matching function for call to ‘std::vector<std::vector<double> >::vector(std::vector<double>&, unsigned int&)’
/usr/include/c++/4.5/bits/stl_vector.h:241:7: note: candidates are: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >, std::vector<_Tp, _Alloc> = std::vector<std::vector<double> >]
/usr/include/c++/4.5/bits/stl_vector.h:227:7: note:                 std::vector<_Tp, _Alloc>::vector(std::vector::size_type, const value_type&, const allocator_type&) [with _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >, std::vector::size_type = unsigned int, value_type = std::vector<double>, allocator_type = std::allocator<std::vector<double> >]
/usr/include/c++/4.5/bits/stl_vector.h:215:7: note:                 std::vector<_Tp, _Alloc>::vector(const allocator_type&) [with _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >, allocator_type = std::allocator<std::vector<double> >]
/usr/include/c++/4.5/bits/stl_vector.h:207:7: note:                 std::vector<_Tp, _Alloc>::vector() [with _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >]

How do I go about declaring Xtrans correctly?

like image 368
user1155299 Avatar asked Mar 07 '26 19:03

user1155299


2 Answers

Besides what the others already said about fixing the code, I'd like to comment on using vector<vector<double> > as a matrix representation which is very inefficient and almost never what you want. A co-worker of mine once inherited a code using this style. Transforming it to simple vector<double> with appropriate index fiddling functions increased the performance by a factor of thirty. Resist the temptation.

You might want to look into one of the many available matrix libraries for C++ (e.g. eigen, uBlas or mtl4 to name a few; there are many others).

like image 70
Michael Wild Avatar answered Mar 09 '26 09:03

Michael Wild


I think there are two problems here - the first is that you might have misunderstood how std::vector's are constructed, and the fact that when you do

std::vector<std::vector<double> >Xtrans(Y,ctr); 

it's generating a compiler error because there is no constructor that matches your declaration.

One of the constructors for std::vector ( i.e. the one you used to declare X ) is declared like this:

explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );

so when you did (ctr, Y) that worked out fine - because you were telling the compiler that you wanted to create a std::vector of size ctr of whatever the value of Y is. (In your case Y is an empty std::vector<double> - so you got a vector of ctr entries, where each entry is an empty std::vector<double>)

So simply swapping ctr and Y in the hopes that you'll get a transposed std::vector is not going to work here.

And the second problem is how you actually transpose the values. You actually need to figure out an algorithm that does the tranpose of X and then pushes those values to Xtrans. Transposing the values is a different thing from actually constructing the vector. Most likely, your algorithm would be something like - construct XTrans, and then iterate over 'Xand insert values intoXTrans`.

like image 40
BeeBand Avatar answered Mar 09 '26 11:03

BeeBand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!