简体   繁体   中英

A vector of matrices using eigen library

using the Eigen library:

I want to create a vector of 4x4 matrices

then loop over it to print an output matrix

THE PROBLEM IS: in the for loop: matrices_vector.size()=0 although I filled matrices_vector with 3 matrices

Thanks all

//Main
vector<MatrixXd> matrices_vector;


    MatrixXd temp(4,4);
    for(int i= matrices_vector.size()-1; i=0; i--)
    {   
        temp= matrices_vector.at(i-1) * matrices_vector.at(i);
        matrices_vector.at(i-1)=temp;
        matrices_vector.erase(matrices_vector.end(),matrices_vector.end()-1);
    }
    cout<< temp;

This is incorrect, as the loop will never execute if i is not 0.

for(int i= matrices_vector.size()-1; i=0; i--)

This should be

for(int i= matrices_vector.size()-1; i > 0; i--)

Additionally, you should check for the matrices_vector being empty before entering the loop, as matrices_vector.size() - 1 will yield an invalid value if matrices_vector.empty() is true .

Also, since your loop checks for pairs, you need to enforce that there are is at least 2 items in the vector before proceeding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM