# Interpolate a vector/array to a different sized vector/array

Hi! Not too sure if this is something simple but I’m not finding a elegant solution.

Suppose I have a vector/array that has 500 elements. I want to apply the values of each of them across 500 rectangles -> simple one to one mapping. But if I want to apply them to 100 or 1000, instead of just doing something like selecting every fifth element or using each element for two successive ones, I want to be able to interpolate/resample the values of the elements into a new array. How do I go about this?

Hi, maybe you can figure something out based on this example?

``````    vector<float> f = {40, 5, 122, 40, 180};

float w = ofGetWidth();
auto segmentCount = f.size() - 1;
float segmentSize = w / segmentCount;
for (int x=0; x<w; x+=5) {
float normalizedPos = x/w;
float tSegment = fmodf(x, segmentSize) / segmentSize;
size_t index = floor(segmentCount * normalizedPos);
float y = ofLerp(f[index], f[index+1], tSegment);
ofDrawLine(x, ofGetHeight(), x, ofGetHeight()-y);
}
``````

Given x, it normalizes it (0…1 range), and figures out which two indices to use from the vector, and interpolates the two.

And if you’re into curves, you could use ofxEasing instead of ofLerp:

``````float y = ofxeasing::map(tSegment, 0, 1, f[index], f[index+1], ofxeasing::quad::easeInOut);
``````

This looks promising! I haven’t had time to play around with this yet, but will do soon! Thanks!