Thread: Problem with a for loop

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    33

    Problem with a for loop

    Hi,

    I have to translate this line from Matlab to C/C++:


    x = (-cols/2 : (cols/2 - 1))/(cols/2);y = -(-rows/2 : (rows/2 - 1))/(rows/2);[x,y] = meshgrid(x,y);
    I try to explain better.

    x is a monodimensional array of lenght=cols.
    y x is a monodimensional array of lenght=rows.

    Cols and rows depends from the picture I charge in the project. (EX cols=696,rows=534).

    x[cols]= all the elements from -(cols/2)/(cols/2) to (cols/2)-1/(cols/2)
    (example) -> x[0] = -(696/2)/(696/2)= -1;
    x[1]= -(695/2)/(696/2)= .-0,99
    ....
    x[695]= 0,99

    For y it s the same. After done this I have to meshgrid. I mean:

    transform the domain specified by vectors x and y into arrays X and Y , that are bidimensional arrays.
    The rows of the output array X are copies of the vector x; columns of the output array Y are copies of the vector y.

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    33
    I tried with :

    Code:
    
        double x_a = -(double)cols/2;  
       double x_b = ((double)cols/2 - 1)/((double)cols/2);
       double y_a = (double)rows/2; 
       double y_b = -((double)rows/2 - 1)/((double)rows/2);
       double m_inc = (x_b-x_a) / m;  
        double n_inc = (y_b-y_a) / n;
    
        double X[rows][cols];  
       double Y[rows][cols];  
     
      for(i = 0 ;  i<=n; i++){     
       for(j= 0;  j<=m; j++) {         
       X[i][j] = x_a + m_inc*j;            
       Y[i][j] = y_a + n_inc*i;       
      }  
      }

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    can you post some code please
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elvio Esposito View Post
    I tried with :

    Code:
    
        double x_a = -(double)cols/2;  
       double x_b = ((double)cols/2 - 1)/((double)cols/2);
       double y_a = (double)rows/2; 
       double y_b = -((double)rows/2 - 1)/((double)rows/2);
       double m_inc = (x_b-x_a) / m;  
        double n_inc = (y_b-y_a) / n;
    
        double X[rows][cols];  
       double Y[rows][cols];  
     
      for(i = 0 ;  i<=n; i++){     
       for(j= 0;  j<=m; j++) {         
       X[i][j] = x_a + m_inc*j;            
       Y[i][j] = y_a + n_inc*i;       
      }  
      }
    what are you using to edit your code? microsoft word?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>for(i = 0 ; i<=n; i++)
    This is a buffer overrun. Indexes go from 0 to size - 1, so if n is size, then n - 1 is the last element.
    Arrays in C++ are tricky, especially since there is no default bounds checking and everyone teaches people not to use practices to avoid those, so have a read.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Elkvis View Post
    what are you using to edit your code? microsoft word?
    I do this sometimes, it's really nice being able to see how it'll look on paper before i print it.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    o_O
    Why do you print code?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jun 2013
    Posts
    33
    I solved with:

    Code:
    double x[cols*2],y[rows*2];
    
     for(int i=0;i<cols*2;i++){
       x[i]=((double)(i-cols)/2)/((double)cols/2);
     }
    The same thing for the y vector.
    Now to do the meshgrid:

    Code:
     double X[cols*2][rows*2];
     double Y[cols*2][rows*2];
    
     for(int m=0;m<cols*2;m++){
        for(int n=0;n<rows*2;n++{
            X[m][n]=x[m];
            Y[m][n]=y[n];
        }
     }
    


    Is it right?

  9. #9
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    Quote Originally Posted by Elysia View Post
    o_O
    Why do you print code?
    Physically annotating code with a red pencil is one of life's joys. Do it in moderation.

  10. #10
    Registered User
    Join Date
    Jun 2013
    Posts
    33
    After changed the loop , print it (work good), after change the other loop, now I have a Segmentation fault a run time I see debugging. It must be about radius and theta. The code:

    Code:
    
    double** radius, **theta;
    ...
    raidus=(double**)malloc(sizeof(double*)*rows;
      for(int i=0;i<rows;i++);
        radius[i]=(double*)malloc(sizeof(double)*cols;
    
    theta=(double**)malloc(sizeof(double*)*rows;
      for(int z=0;z<rows;z++); 
        theta[z]=(double*)malloc(sizeof(double)*cols;
    The function where they are used I modified. When I arrive to print function I have the segmentation fault debugging

    Code:
    void pre_filter_Computations(double **radius,double **theta,int cols,int rows){
    
    double x[cols],y[rows];
    double X[cols][rows],
     Y[cols][rows];
    double epsilon=0.0001;
    
     printf("Entering prefilter function\n");
     
    for(int i=0;i<cols*2;i++){  
        x[i]=((double)(i-cols)/2/((double)cols/2; 
    }
    ...

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    is there a really good reason why you're using pointer-to-pointer style arrays instead of std::vector<std::vector<double> >?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    More cross-posting buffoonery.
    Segmentation Fault At Run Time - Dev Shed
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Jun 2013
    Posts
    33
    The first reason is that in my University we studied just C and not C++. But my work project is about image processing. So I have to use OpenCV library that works with OpenCV. So I don' t know well the type of variable. For what I just see I suppose it' very usefull in my case. What I have to do it s to build in this function the 2 matrix theta(angle) and radius to speed up filter construction. If I use <std::vector<double> I can go to every part of the vector as it be a bidimensional array? Because after I have to square all ellement of radius and to change the center value of matrix radius to 0 and do some more stuff. Thanks helping

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A vector is an array with dynamic size. Therefore, yes, you can access any element.
    You need to brush up on your C++. When you need to do something, look for the available C++ containers and algorithms.
    And whatever you do - don't use malloc! It will cause your program to blow up in your inexperienced hands with C++. There are places where one might have to use it, but this is not one of them. Always use new/delete instead of malloc/free in C++ unless there is a special requirement.
    But to go even further, don't use new at all if you can. There are lots of containers that all the hard work for you (eg vector). Even if there are no containers, you can still use smart pointers.
    And if the standard library don't have what you're looking for - don't forget to try the Boost library!

    And I did give you a link before about arrays, which I am pretty convinced you did not read. It is important that you do.
    If you don't understand all of it, then that's fine. Ask a question or go research a little on the topic you don't understand, but be sure to understand what it says before dealing with arrays. Arrays are tricky things - especially to a C developer who has no knowledge of the alternatives to make them safe in C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jun 2013
    Posts
    33
    I have a question: Can I assign a bidimension array of double to an array of double;

    double X[cols][rows];
    vector<double>radius(rows*cols);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do...While Loop problem
    By Paul Adams in forum C Programming
    Replies: 2
    Last Post: 03-27-2012, 12:49 PM
  2. loop problem
    By sababa.sababa in forum C Programming
    Replies: 10
    Last Post: 02-17-2010, 06:53 AM
  3. Plz help with this loop problem,Thanks!
    By blue_blue in forum C Programming
    Replies: 4
    Last Post: 04-28-2008, 11:34 PM
  4. problem with 'for' loop
    By kirklandkonnect in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2006, 10:03 PM
  5. while loop problem
    By tortan in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2006, 11:11 AM

Tags for this Thread