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

  • Home
  • SEARCH
  • 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 4115780
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:37:33+00:00 2026-05-20T22:37:33+00:00

I understand there are issues when you try to change size of 2d vector

  • 0

I understand there are issues when you try to change size of 2d vector objects but I’m facing a rather weird issue here. Code fragment is attached below :

for (int iter = 0; iter < J; iter++) {
                cout << "iter " << iter << endl;
                rows_n = rows_n / 2;
                cols_n = cols_n / 2;
                cout << "sig2 " << sig2.size() << endl;

                vector<vector<double> >  cA(rows_n, vector<double>(cols_n));
                vector<vector<double> >  cH(rows_n, vector<double>(cols_n));
                vector<vector<double> >  cV(rows_n, vector<double>(cols_n));
                vector<vector<double> >  cD(rows_n, vector<double>(cols_n));
                cout << "cA " << cA.size() << endl;

                ANALYSIS_FB(nm,sig2,cA,cH,cV,cD);

                sig2.clear();
                vector<vector<double> >  sig2(rows_n, vector<double>(cols_n,0.0));

                sig2 = cA;

                for(int i =0; i < rows_n; i++){
                    for (int j =0; j < cols_n; j++){
                        dwt_output[i][j]=cA[i][j];
                    }
                }
                for(int i =0; i < rows_n; i++){
                    for (int j = cols_n; j < cols_n * 2; j++){
                        dwt_output[i][j]=cH[i][j - cols_n];
                    }
                }

                for(int i = rows_n; i < rows_n * 2; i++){
                    for (int j =0; j < cols_n; j++){
                        dwt_output[i][j]=cV[i - rows_n][j];
                    }
                }

                for(int i = rows_n; i < rows_n * 2; i++){
                    for (int j = cols_n; j < cols_n * 2; j++){
                        dwt_output[i][j]=cD[i- rows_n][j - cols_n];
                    }
                }
                for(int i = 0; i < rows_n; i++){
                    for (int j = 0; j < cols_n ; j++){
                        cout << sig2[i][j] << " ";
                    }
                    cout << endl;
                }

           cA.clear();
           cH.clear();
           cV.clear();
           cD.clear();

            cout << "sig2e  " << sig2.size() << endl;
            }

As you can see this is part of a much larger code but my problem is that 2d vector vanishes after the first iteration of the loop. I’m attaching the output from the console as well.

iter 0

sig2 16

cA 8

4.49991 10.4998 16.4997 22.4996 28.4995 34.4993 40.4992 46.4991
8.99983 20.9996 32.9994 44.9991 56.9989 68.9987 80.9984 92.9982
13.4997 31.4994 49.4991 67.4987 85.4984 103.498 121.498 139.497
17.9997 41.9992 65.9987 89.9983 113.998 137.997 161.997 185.996
16.4997 38.4993 60.4988 82.4984 104.498 126.498 148.497 170.497
14.9997 34.9993 54.9989 74.9986 94.9982 114.998 134.997 154.997
16.4997 38.4993 60.4988 82.4984 104.498 126.498 148.497 170.497
13.4997 31.4994 49.4991 67.4987 85.4984 103.498 121.498 139.497

sig2e 8 (My Note – First Iteration Ends with sig2’s number of rows = 8)

iter 1 ( My Note – Second Iteration Begins)

sig2 0 (My Note- sig2 ‘s number of rows = 0)

cA 4

As you can see, sig2 takes the value of cA in the first iteration and has the size 8 X 8 just before the end of the loop is reached. For the next iteration (iter value of 1), the 2d vector becomes a null vector. To be fair , I’m clearing the vector in the first iteration , followed by redefining it to be a smaller size but I still find the behavior to be a bit odd. Is there a way around this? The function ANALYSIS_FB takes the value of sig2 and returns cA,cH etc. sig2 ‘s value is set to cA (which has dimensions half of the original sig2 in both directions) and so on.

Edit : Thank you guys for pointing out that sig2 was a global as well as the local variable. I had hit the wall and that helped me solve the problem. I ended up removing all the “clear” functions and getting rid of any additional local variables. I am using sig2 only as the global variable and the program is working.

  • 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-05-20T22:37:34+00:00Added an answer on May 20, 2026 at 10:37 pm

    You have 2 varibales named sig2, one outside of the loop and one inside !

    From your code :

    ///You clear a global sig2
    sig2.clear();
    ///You create a local sig2 !
    vector<vector<double> >  sig2(rows_n, vector<double>(cols_n,0.0));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that floating point calculations have accuracy issues and there are plenty of
I understand how editing rows can cause concurrency issues, but concurrency issues being caused
I understand there is a HTTP response header directive to disable page caching: Cache-Control:no-cache
Something I'm not to clear about, I understand there are differences between C# and
I understand that there are several ways to blend XNA and WPF within the
I understand that there's no universal answer to the attribute vs. element debate (and
This question is for the java language in particular. I understand that there is
I understand the following elements : there is some sort of crawling (configured in
is there any way to make IE6 understand double classes, say I have a
Is there a way that I can configure Visual Studio 2008 to understand CamelCase?

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.