Registered Member
|
Hi,
I am new to libeigen and have recieved the task to port some matlab code snippets to C++. I have read the Core Tutorialbut have some questions about how to reimplement few Matlab functions for use with eigen (if they are not already available..) repmat [Link to Doc] B = repmat(A,m,n) creates a large matrix B consisting of an m-by-n tiling of copies of A. The size of B is [size(A,1)*m, (size(A,2)*n]. The statement repmat(A,n) creates an n-by-n tiling. I think in pseudocode it should be reproducable by something like ones(m,n).*.A , but what is the best way to do it in eigen, using VectorXf as example. meshgrid [Link to Doc] Is there a simpel way to reproduce a matlab call like [X,Y] = meshgrid(1:3,10:14) without loops or something? The first problem I see is returning 2 matrices from a function, the second is I haven't found the matlab ":" operator, and I think I can only emulate it using a loop. reshape [Link to Doc] B = reshape(A,m,n) returns the m-by-n matrix B whose elements are taken column-wise from A. An error results if A does not have m*n elements. Is any of this functionality already present in eigen and could I only not find it? Or has anyone suggestions on how to implement them in an elegant way? regards, Ismael |
Moderator
|
repmat
e.g., dst = src.replicate(n,m); see http://eigen.tuxfamily.org/dox-devel/cl ... 4e4b103032 meshgrid you can mix LinSpaced and replicate: X = RowVectorXd::LinSpaced(1,3,3).replicate(5,1); Y = VectorXd::LinSpaced(10,14,5).replicate(1,3); see: http://eigen.tuxfamily.org/dox-devel/cl ... a9c6ae34d8 reshape Currently you can mimic it using Map: B = Map<MatrixXd>(A.data(),m,n); |
Registered Member
|
|
Registered Member
|
Thanks for your fast help and explanations FMD and ggael
The QuickReference is realy great. One thing however I couldn't find out myself. I have to work most of the time with dynamic-sized vectors and need to do something like this in matlab (simplified example):
so I can use VectorXi b = VectorXi::Ones(10,1)*17 or VectorXi b = VectorXi::Constant(10,1,17); to compute the second part. But how can I then combine them to one? I could not find anything in the QuickReference as it mostly shows operations with matrices. Do I have to manually resize a to the length of a+b, and then copy the values over in a loop? This seems to be a costly operation, and since I have to do it thousand of times I would like to implement it the right way. thanks, Ismael |
Registered Member
|
You can do:
if the result is not the same matrix as a. However if it is a itself that you are extending, with eigen 3, you can do this:
Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list! |
Registered Member
|
Looks like this is affected by the matrix order. If A uses column-major, I don't think that your technique will produce the desired result. Can you please confirm?? |
Registered Member
|
I'm including source code to show this is the case:
This is wrong. The last result should be:
Instead of:
Any clue on how to get the right result?? |
Registered Member
|
In Eigen3 you can make use of the Stride object Stride<Dynamic,Dynamic> stride(3,6); const auto B = EigenMatrixDbl::Map(m.data(), 2, 2, stride); but again, your result will depend on the memory layout. Right now we don't have a "real" reshape. - Hauke |
Registered Member
|
Thanks, but I use 2.0.15. Version 3 is still in Beta.
This is what I came up with:
Messy, but seems to work More generically, for a matrix, the following code should give the correct reshape:
|
Registered Member
|
Hi,
I too am looking for a conversion from Matlab to Eigen on repmat, specifically this line. X = X ./ repmat( sum(X, 1), size(X, 1), 1 ); My initial thoughts were to use a cwiseQuotient and a replicate with the relevant eigen reduction calls but this doesn't seem to be working. Any ideas would be great! Thanks, Kris |
Moderator
|
What about the following (not tested:
|
Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]