Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8049871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:54:25+00:00 2026-06-05T06:54:25+00:00

What is the most elegant and efficient way to convert a nested std::vector of

  • 0

What is the most elegant and efficient way to convert a nested std::vector of std::vectors to cv::Mat? The nested structure is holding an array, i.e. all the inner std::vectors have the same size and represent matrix rows. I don’t mind the data being copied from one to another.

I know that a single, non-nested std::vector is very easy, there is a constructor:

std::vector <double> myvec;
cv::Mat mymat;

// fill myvec
bool copy = true;

myMat = cv::Mat(myvec, copy);

What about the nested vector?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:54:26+00:00Added an answer on June 5, 2026 at 6:54 am

    This is a way to do it:

    std::vector<double> row1;
    row1.push_back(1.0); row1.push_back(2.0); row1.push_back(3.0);
    
    std::vector<double> row2;
    row2.push_back(4.0); row2.push_back(5.0); row2.push_back(6.0);
    
    std::vector<std::vector<double> > vector_of_rows;
    vector_of_rows.push_back(row1);
    vector_of_rows.push_back(row2);
    
    // make sure the size of of row1 and row2 are the same, else you'll have serious problems!
    cv::Mat my_mat(vector_of_rows.size(), row1.size(), CV_64F);
    
    for (size_t i = 0; i < vector_of_rows.size(); i++)
    {   
        for (size_t j = 0; j < row1.size(); j++)
        {   
            my_mat.at<double>(i,j) = vector_of_rows[i][j];
        }   
    }   
    
    std::cout << my_mat << std::endl;
    

    Outputs:

    [1, 2, 3;
      4, 5, 6]
    

    I tried another approach using the constructor of Mat and the push_back() method but without success, maybe you can figure it out. Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the most efficient/elegant way to filter all paths by a base path?
What would be the most elegant and efficient way of finding/returning the first list
What is the easiest or most elegant way to convert a RFC 1123 date
I'm wondering if this is the most elegant and efficient way of doing this?
What would be the most efficient\elegant way in Python to find the index of
Possible Duplicate: What is the most efficient/elegant way to parse a flat table into
What is the most efficient/elegant way to dump a StringBuilder to a text file?
What would be the most elegant way to turn any array of (equal length)
What´s the most efficient, elegant and pythonic way of solving this problem? Given a
What's the most elegant way to get this array [10, 20, 30, 40, 50]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.