I have MatrixXf A & MatrixXf B and I want to create a new matrix MatrixXf C with max values for each index i.e.
C(i,j) = max(A(i,j), B(i,J);
Does Eigen have a function to do this?
A.cwiseMax(B) can also be used.
Check http://eigen.tuxfamily.org/dox/group__QuickRefPage.html#title6 for reference
Related
How do I perform a matrix vector product using the Eigen library in C++ with a "for loop", when the date type of my variables are in the form Eigen::MatrixXd and Eigen::VectorXd?
NOTE: I don't want to use the in-built * operator of directly multiplying a matrix with vector, I want to use a for loop and perform the element wise operation myself
When I try to do the element wise multiplication on Eigen::MatrixXd and Eigen::VectorXd, I get an error.
I have no idea what problem you have encountered. Here is an example that may help.
// mat: 3x4 vec_h: 4x1
Eigen::MatrixXd mat = Eigen::MatrixXd::Ones(3,4);
Eigen::VectorXd vec_h = Eigen::VectorXd::Random(4);
// mat.row(i): 1x4
// mat.row(i).transpose(): 4x1
// cwiseProduct() element-wise multiplication
// add .eval() because we are changing the element value in place.
for (int i = 0; i < mat.rows(); ++i) {
mat.row(i)=mat.row(i).transpose().cwiseProduct(vec_h).eval();
}
In case you are looking for more information, here's the link Eigen: The Array class and coefficient-wise operations
I have two Eigen vectors (vectorOne and vectorTwo) of my defined type( see below for my type).
typedef Matrix<double, 50, 1> myVector;
I want a third vector vectorThree that will have multiplication of vectorOne and vectorTwo. But I want to multiply each element by corresponding element - i.e. vectorOne(i, 0) by vectorTwo (i, 0) so that I have something like below for all i.
vectorThree (i, 0) = vectorOne(i, 0) * vectorTwo(i, 0)
I saw this and tried vectorOne.array() * vectorTwo.array() but it did not work.
I know I can do that using a for loop and iterating over all elements. But is there a more efficient or built in Eigen function for that?
You should be able to cast matrices to arrays via .array() and multiply it here. It would return an array expression though, so maybe it is not what you want.
From Eigen documentation:
First of all, of course you can multiply an array by a scalar, this works in the same way as matrices. Where arrays are fundamentally different from matrices, is when you multiply two together. Matrices interpret multiplication as matrix product and arrays interpret multiplication as coefficient-wise product. Thus, two arrays can be multiplied if and only if they have the same dimensions.
Otherwise you can use .cwiseProduct of matrix to get matrix as result.
https://eigen.tuxfamily.org/dox/group__QuickRefPage.html#matrixonly
I'm using the Armadillo Cpp code for matrix algebra. I have an Eigenvector matrix E that I want to sort by its eigenvalues in a vector d.
mat E;
vec d;
eig_sym(d,E,Rxx);
// Sort indices of eignen values / vectors
// based on decreasing real part of eigen values.
uvec order = sort_index(-d);
// Extract top eigen vectors.
E = E(span::all,order(1,nb_sources));
I couldn't find anything related to this kind of indexing in the documentation. Indexing using a vector is such a common requirement that I'd be surprised if it isn't present in Armadillo.
What is the proper way to do this in Armadillo?
One way to do it is
E = E.cols(order(span(0,nb_sources-1)));
I switched from matlab to C++ to write a CFD solver. I am using Eigen linear algebra library. It has many function for the Matrix and Vector manipulations but it's lacking in functions to convert Matrix to Array.
MatrixXf m(2,2);
m<<1,2,3,4;
ArrayXf a(4);
a=m.array();
This is the solution I have for this
m.resize(4,1);
a=m;
I don' like this because the m is changed, which I don't want because m is a very big matrix.
If you don't want to copy the values you can use an Eigen::Map like so:
MatrixXf m(2,2);
m<<1,2,3,4;
Eigen::Map<ArrayXf> mp(m.data(), m.size());
You can then use mp as an ArrayXf. Note that this points to the original m matrix, i.e. changes to mp will be present in m. If you want a copy, you can use:
ArrayXf a = mp;
You could write your own function and copy value by value.
MatrixXf matrix(2,2);
int cols=matrix.cols();
int rows=matrix.rows;
ArrayXf array(cols*rows);
for(int c=0;c<cols;c++)
for(int r=0;r<rows;r++)
array(c*rows+r)=matrix(c,r);
I want to multiply 5 matrix ( all are not of same size) in c++, so what i can do?
will i have to use loop 5 times or is there any simple method like as in matlab?
sizes of matrix are
1st : 1x4
2nd : 4x4
3rd : 4x4
4th : 4x4
5th : 4x1
You could use boost linear algebra library which defines both matrix and vector types and has multiplication with operator *.
matrix<int> m1(1,4);
matrix<int> m2(4,4);
matrix<int> m3(4,4);
matrix<int> m4(4,1);
//... initialize your matrices here
matrix<int> result = m1*m2*m3*m4;
Write a function that performs the matrix multiplication for matrices of arbitrary size (with a sanity check).
Use the function four times, once for each multiplication.
Alternatively, you can define the * operator for the type matrix that you defined yourself, so that you could just write a*b*c*d