making a grid of a vector

Sep 5, 2019 at 6:30pm
I have a vector, let us call it x.

I want to make a grid of points between 1 and a variable, let us call the variable xm.

How can I make a grid between 1 and xm of the vector (x) with total number of points (nump), so that the points are evenly spaced between 1 and xm?


 
  for (int i=0;i<nump;i++)


Clearly, I am not sure what to do. How do I make a grid?
Last edited on Sep 5, 2019 at 6:31pm
Sep 5, 2019 at 6:41pm
the number of points is just a computation.
since you started at 1, its not a hard one:
xm/numpoints. eg, if you had xm = 1000 and you want a point every 40 ticks, you need 25 points. or if you know the # of points, just use it. When making the # of points, if you compute it, you have to decide if you are +1 or not (if the computation is 40.012 do you use 40 points or 41 points?) ... this is a choice depends on what you are doing.

vectors can resize but for things like this set it up front:
vector<int> x(25);//25 points -- unlike arrays, this can be a variable
for(int i = 0; i < x.size(); i++)
x[i] = something;

this is not a grid though. It sounds like an interpolation table.
a grid is 2-d usually, what are you asking really?
Last edited on Sep 5, 2019 at 6:44pm
Sep 5, 2019 at 6:47pm
1
2
delta = (high-low) / (n-1)
x[j+1] = x[j]+delta //x_1 = low; x_n = high 
Sep 5, 2019 at 6:49pm
Yes, I want an interpolation table. I don't know what Xm is, it is specified by the user.

Why does this not work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "pch.h"
#include <iostream>
#include <utility>
#include <vector>
int main()
{
	double nump = 100;
	double xm = 1.7;
	std::vector<double> x;

	for (int i = 0; i < nump; i++)
	{
		double s = (xm - 1.0) / nump;
		x[i+1] = 1.0 + i * s;
		std::cout << x[i+1] << std::endl;
	}
}


It says the vector subscript is out of range
Last edited on Sep 5, 2019 at 6:51pm
Sep 5, 2019 at 6:51pm
Your vector has 0 size. You are also setting x[i+1] but printing x[i], which looks inconsistent. (Edit: you fixed that latter point)

If you made your vector have an initial size of nump, then you could access x[i] correctly.
std::vector<double> x(nump);
1
2
x[i] = ...;
cout << x[i];


Change nump to an integer.
Last edited on Sep 5, 2019 at 6:53pm
Sep 5, 2019 at 7:16pm
I said that :)
vector<int> x(25);//25 points -- unlike arrays, this can be a variable


vector<int> x;
this is very, very similar to {int*x; }
vector<int> x(100);
this is very, very similar to {int*x = new int[100];}

one of those will blow up if you try to use x[index] = value and the other will let you use index from 0-99 .... its the same idea. vectors are a wrapper for 'dynamic arrays' (and a stack class, as an intentional side effect). They have a safe out of bounds call (.at()) but its slow. Better to just be aware of what is going on and carefully use [] notation.

you could also pushback() every value. I dislike this approach, but its more or less the same if you reserve() your 100 first, and risks a memory reallocation/ data copy if you don't. You will see this a lot in other people's code. Its most handy if you truly do not know what you are going to get and cannot even over-guess, like reading a file or user input.
Last edited on Sep 5, 2019 at 7:21pm
Sep 5, 2019 at 9:04pm
> x[i+1] = 1.0 + i * s;
also, the first number in the vector is x[0]
so that should be written x[i] = 1.0 + i * s;
Sep 5, 2019 at 9:13pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>
int main()
{
	int nump = 100;                             // <===== int
	double xm = 1.7;
	std::vector<double> x(nump);                 // <===== declare size

	for (int i = 0; i < nump; i++)
	{
		double s = (xm - 1.0) / ( nump - 1 );     // <===== nump - 1
		x[i] = 1.0 + i * s;                       // <===== x[i]
		std::cout << x[i] << '\n';
	}
}



or you could try
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <valarray>
#include <numeric>
using namespace std;

int main()
{
   int nump = 100;
   double xm = 1.7;
   valarray<double> x(nump);
   iota( begin( x ), end( x ), 0.0 );
   x = 1 + x * ( xm - 1 ) / ( nump - 1 );
   for ( auto e : x ) cout << e << '\n';
}
Last edited on Sep 5, 2019 at 9:54pm
Topic archived. No new replies allowed.