Registered Member
|
Hi Guys!
Can eigen work with 3d matrices? Im currently rewriting matlab code in c++ and i need to implement the meshgrid(x, y, z) function. Can anyone help me out? Would be really appreciated |
Moderator
|
Eigen does not support multi-dimensional arrays or tensors. So you'll have to mimic it with a vector of 2D matrices, and setLinSpaced.
|
Registered Member
|
I'll post the matlab code and maybe you'll see the problem im having
I dont know how i would calculate the euclidean norm ( d=sqrt(i.^2+j.^2+k.^2) ) in this way.... Seems like working with a vector of matrices wouldnt be very friendly. Also the code needs to run as fast as possible, the ideal being almost in realtime. |
Moderator
|
Not tested, but that's the general idea:
|
Registered Member
|
|
Registered Member
|
I've been looking at your code and i dont think this does the same thing as matlabs meshgrid function. I'm really lost
Edit: I understand that i want to create an array of matrices for this, but i dont understand your code. what is x2y2? And why is d a vector sized N^3, when its supposed to be a Matrix sized N^3? I kind of imagine working with a variable like this: NxN NxN NxN v = ( . ) . . NxN So v is an Array of matrices. Is this possible in eigen? |
Moderator
|
Since you only apply coefficient-wise operations on d, c, r, etc. it is finally simpler to represent them as 1D arrays. Moreover, at this end your 3D matrices are linearized anyway.
x2y2 is a N^2 array where x2y2(i+j*N) = x_i^2 + y_j^2. The same technique is used to compute d from x2y2. And you keep using the same approach to compute c. The other operations are straightforward. |
Registered Member
|
Ok I understand now and could extrapolate from this, but im hitting a breakpoint when i debug the application for some reason and i dont know why...
Heres the code, i know it could be more efficient, but as i said im pretty new to all this and am trying my best
|
Registered Member
|
I would be really thankful for some help!
ArrayXXf::Map(x2y2.data(), N, N) = x.square().replicate(1,N) + y.square().transpose().replicate(N,1); when you replicate x and y they become matrices and then you map them to a vector again. How does this work? I'm pretty sure the error im getting is to do with vector dimensions not matching up! Thank you so much! |
Moderator
|
It's "zk.square().transpose().replicate(N*N,1)" and not "zk.square().transpose().replicate(N,1)".
|
Registered Member
|
Yep thanks! I actually managed to work it out yesterday too, but it took quite a while. So for future reference:
Thanks again for the help! |
Moderator
|
btw, if at the end you can come up with some speed comparison with the initial MatLab code, that would be nice!
|
Registered Member
|
I'll see if i get around to it and will post results if i do!
|
Registered users: Bing [Bot], Google [Bot], Sogou [Bot], Yahoo [Bot]