-
April 22nd, 2012, 08:06 PM
#1
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.
-
April 22nd, 2012, 10:10 PM
#2
Re: Multi-Dimension Vectors
Originally Posted by hfbroady
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
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.
-
April 23rd, 2012, 03:49 AM
#3
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?
-
April 23rd, 2012, 04:41 AM
#4
Re: Multi-Dimension Vectors
Originally Posted by hfbroady
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
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
-
April 23rd, 2012, 07:42 AM
#5
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.
-
April 23rd, 2012, 08:18 AM
#6
Re: Multi-Dimension Vectors
Originally Posted by hfbroady
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.
-
April 23rd, 2012, 11:18 AM
#7
Re: Multi-Dimension Vectors
-
April 23rd, 2012, 11:35 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|