Given a array like this:
{1, 3, 11, 2, 24, 13, 5....}
The array length might be larger than 1,000.
If the value of the element is inappropriate, such as larger than 10, it shall be replaced by appropriate value. In this case, the appropriate value is calculated by linear interpolation.
For example:
Arr = {1, 3, 11, 2, 24, 13, 5....};
The new array shall be:
NewArr = {1, 3, 3+(2-3)/2, 2, 2+(5-2)/3, 2+2*(5-2)/3, 5, ...}
In order to do this, I have to know the starting and ending index of the inappropriate elements.
The starting and ending index shall be (2,2) indicating the "11" and (4,5) indicating the "24, 13"
I have tried the for loop
. But it is not efficient.
Then I searched IPP API
and did not get an result. :(
Is there a better idea?
Thanks for your help, :).
BTW: IPP API
will be a better choice.
Update:
Sample Codes:
int arr[] = {1, 3, 11, 2, 24, 13, 5....};
/// find the starting index and ending index of inappropriate values
/// (2,2) (4,5).
int i = 0;
std::map<int,int> Segments;
if(arr[i] > Threshold)
{
int b = i;
while(arr[i] > Threshold )
i ++;
int e = i;
Segments.insert(std::map<int,int>::value_type(b,e));
}
/// linear interpolation
for(std::map<int,int>::iterator i = 0; i != Segments.end(); i ++) /// len means the number of inappropriate segments
{
//// linear interpolation of each segments
int b = i->first;
int e = i->second;
int num = e - b + 1;
float step = (arr[e+1]-arr[b-1]) / num; // For short. The case that b=0 or e=len-1 is not considered.
for(int j = b; j <= e; j ++)
arr[j] = arr[j-1] + step;
}
Update2:
Thanks for all your help. But based of the answers of these questions:Speed accessing a std::vector by iterator vs by operator[]/index? and Why use iterators instead of array indices?, the efficients of two forms( for vs iterator) are almost the same. So iterator
might not be good enough.
I usually used the SIMD
such as IPP API
as the optimize option. But I did not figure it out since all the find
API only get the first occurrence of the specified element.
I will update the solution if I figure out some day. :)