CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2011
    Posts
    6

    Multi-Dimension Vectors

    Ok I'm trying to create matrix of data that I can add values to based on a reading that I will get from a DVM. I'm using a 3d vector to do this. I need to keep track of the serial number, the day and within day I need to take two readings at different temps, this will continue for 7 days.

    The rows of my 3d vector will be the days, the colums will be the serial numbers the depth will be the reading at the different temps.

    What I need to do is compare the first element (days) and when it is greater then or equal to 4 I will perform calculations of the readings. So all I want to do is compare the first element of the vector, How do I do this?

    Here is what I got


    Code:
    #include <vector>
    typedef std::vector<double> Double1D;
    typedef std::vector<Double1D> Double2D;
    typedef std::vector<Double2D> Double3D;
    
    #define HEIGHT 5
    #define WIDTH 3
    #define DEPTH 7
    
    int main() 
    {
       Double3D array3D(HEIGHT, Double2D(WIDTH, Double1D(DEPTH)));
      // Put some values in
      // The second element will have 2 readings for each temp so the depth will be 2*5=10 but the last four only used for day 7
    
         array3D[3][1][4] = 5.5; // this is for the 20k pots
      
      return 0;
    }
    Last edited by hfbroady; April 23rd, 2012 at 03:45 AM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Multi-Dimension Vectors

    Quote Originally Posted by hfbroady View Post
    What I need to do is compare the first element (days) and when it is greater then or equal to 4
    First, you can clean up your code:
    Code:
    #include <vector>
    typedef std::vector<double> Double1D;
    typedef std::vector<Double1D> Double2D;
    typedef std::vector<Double2D> Double3D;
    
    #define HEIGHT 5
    #define WIDTH 3
    #define DEPTH 7
    
    int main() 
    {
       Double3D array3D(HEIGHT, Double2D(WIDTH, Double1D(DEPTH)));
       //..
    That entire code replaces all of those loops. Also note the typedefs to clean up the template code.
    The rows of my 3d vector will be the days, the colums will be the serial numbers the depth will be the reading at the different temps.
    There are no "rows" or "columns" or "depths". A vector within a vector is whatever you want it to denote. Someone may interpret
    Code:
    array3D[x][y][z]
    As "x" being the depth, and y,z being the rows and columns.

    So which dimension of the 3d vector is the "row", which one is the "column" and which one is the "depth"? Also, what does "comparing the first element" mean? You have a vector with 3 indices, so which one is the first?
    Code:
    while( array3D[1][0][0] == 1)
    Is this the first element? Arrays and vectors start at index 0, not 1.
    Code:
    while( array3D[0][0][0] == 1)
    That would make a little more sense in comparing the "first element".

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 22nd, 2012 at 10:12 PM.

  3. #3
    Join Date
    May 2011
    Posts
    6

    Re: Multi-Dimension Vectors

    I cleaned the code up a bit. The first element that I'm talking about well i guess its not an element because its a vector inside vector inside a vector. So what I'm trying to do is comapre the values in the first vector only.... example array3d[1][0][0]. The vector witht the one in it is have I want to compare to see if it is greater than equal to 4?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Multi-Dimension Vectors

    Quote Originally Posted by hfbroady View Post
    I cleaned the code up a bit. The first element that I'm talking about well i guess its not an element because its a vector inside vector inside a vector. So what I'm trying to do is comapre the values in the first vector only.... example array3d[1][0][0]. The vector witht the one in it is have I want to compare to see if it is greater than equal to 4?
    That is the second vector, not the first. The one I posted is the actual first vector.

    Also, the issue really has nothing to do with vector -- it has everything to do with how you are interpreting a 3-dimensional array. If you can't "picture" the 3-d array in your mind, then you aren't going to understand what each index denotes.

    It is the rightmost index that actually has the individual data item. The second index is a "pointer" to the vector that contains the data items. The leftmost index is a pointer to the pointer to the data items. So
    Code:
    array3d[0][0][0]
    is the first double in the innermost vector, the middle 0 is the first vector of these items, and the leftmost 0 is the first vector of the middle set of vectors.

    Why not start with the simple example I posted? Fill it with values, and then write a small program to figure out where things are.

    Also, you could simplify all of this if you used iterators and the typedefs I had in my example.
    Code:
    #include <vector>
    typedef std::vector<double> Double1D;
    typedef std::vector<Double1D> Double2D;
    typedef std::vector<Double2D> Double3D;
    
    #define HEIGHT 5
    #define WIDTH 3
    #define DEPTH 7
    
    int main() 
    {
       Double3D array3D(HEIGHT, Double2D(WIDTH, Double1D(DEPTH)));
    
       // Declare an iterator that points to the first innermost element
       Double1D::iterator it = array3D[0][0].begin();
    
       // loop through this vector
       while (it != array3D[0][0].end())
       {
            *it = 10.0;
            ++it;
        }
       
       // Declare an iterator that points to the second middle vector, and points to the first innermost element
       iterator it = array3D[0][1].begin();
       // loop through this vector
       while (it != array3D[0][1].end())
       {
            *it = 20.0;
            ++it;
        }
    So when you run this program (preferably in a debugger), do you see where the 10's and 20's are located?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Multi-Dimension Vectors

    Let's just go back 1 dimension for now, to make things simpler:

    Code:
    #include <vector>
    typedef std::vector<double> Double1D;
    typedef std::vector<Double1D> Double2D;
    
    #define HEIGHT 5
    #define WIDTH 3
    
    int main()
    {
      Double2D array2D(HEIGHT, Double1D(WIDTH, 0));
    
      //This is the first "row":
      Double1D& first_row = array2D[0];
    
      //This is the second "row":
      Double1D& second_row = array2D[1];
    
      //This is the first element of the second row:
      double a = array2D[1][0];
      //double a = second_row[1]; //equivalent
    }
    In reality there is no concept of "rows" or "columns", but merely order of indexing. In this case, we have a vector holding HEIGHT amount of vectors being WIDTH large.

    Nothing stops you from indexing the other way around, as long as you are homogenous.

    Personally, I like sticking to the matrix scheme
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Multi-Dimension Vectors

    Quote Originally Posted by hfbroady View Post
    I'm using a 3d vector to do this.
    Multi-dimensional arrays are so BASICish.

    In C++ it's more common to store compound data in structs or classes, like say

    Code:
    struct Measure {
       long serialNo;
       int days;
       vector<double> data;
    };
    Then you can store Measure objects in an ordinary one-dimensional vector or maybe a map (with serial number as key).
    Last edited by nuzzle; April 23rd, 2012 at 03:50 PM.

  7. #7
    Join Date
    Jan 2012
    Location
    toronto
    Posts
    13

    Re: Multi-Dimension Vectors


  8. #8
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: Multi-Dimension Vectors

    On a side note, The Boost Multidimensional Array Library may provides much more and really easy to use.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured