05-02-2012 02:54 PM
Hello,
I hope somebody has faces a similar problem and will be able to help.
I have a large 1D array of scalars. The first few elements (not know how many) of the array are "zeros" and the last few elements (also, not known how many) are a constant value. These final values is close to 90, but actually the value is not exactly known. The data between the initial "zeros" and the final constant values are in the range between 0 and 180. Graphically, the data represents a dapmped sinusoidal oscillations.
Now is the question: What is the most efficient way to remove the initial "zeros" and the final constant values from my large array?
I searched the posts and found that the "zeros" can be removed by following the procedure (see the link):
The above procedure works in my case, but, since the procedure assumes that "zeros" may appear anywhere in the array, the time for completing my array of data is significant. I wonder if the procedure can be steamlined for my case where the "zeros" are only in the beginning of the array. Also, how can I incorporate the removal of the final constant values?
Thank you for help
Best
Pavel
05-02-2012 03:24 PM
Hi Pavel,
As with most things to do with array manipulation, there are probably 101 ways to do this. If you want to minimise cycling through the whole array without prior knowledge of the where the contant values lie, I'd try something like the following:
Basically - look for zeros at the beginning, and work from the back of the array to check when something 'not constant' occurs.
05-02-2012 04:00 PM - edited 05-02-2012 04:05 PM
@Pete.Dunc wrote:
Hi Pavel,
As with most things to do with array manipulation, there are probably 101 ways to do this. If you want to minimise cycling through the whole array without prior knowledge of the where the contant values lie, I'd try something like the following:
And your way requires you to pay me another nickel in my retirement fund. for using comparison operations with floating point values. That's a no-no. (http://stackoverflow.com/questions/1089018/why-cant-decimal-numbers-be-represented-exactly-in-binary...
The correct method is to use a custom check which looks to see if something is "virtually" zero, or "virtually" the same as another value. Numerous ways of doing this have been posted before.
05-02-2012 04:14 PM
05-02-2012 04:18 PM - edited 05-02-2012 04:21 PM
You must be careful not to remove valid data where two consecutive values are found in the middle of the test results. Reverse the array and only take out the constants at the end of the data. As smercurio_fc suggests you cannot use =0, I've used a noise limit for checking equalities.
05-02-2012 04:33 PM - edited 05-02-2012 04:41 PM
05-02-2012 04:56 PM
05-02-2012 04:57 PM - edited 05-02-2012 05:07 PM
Pavel was asking for an efficient method for large arrays :
"What is the most efficient way to remove the initial "zeros" and the final constant values from my large array?"
with ben64's single loop example you are indexing through the whole array, with a very large array this could be taking too much time.
edit:
looks like ben64 posted with his new code while I was composing my reply.
05-02-2012 05:04 PM
I know, I wasn't going for efficiency but for the challenge of finding a way to do it with one loop. I added a solution using 2 loops that doesn't go through all the array. Also note that it is preferable to use the in range function to compare double instead of greater than since the offset can be positive or negative.
Ben64
05-03-2012 11:40 AM
Thank you all for your feedback. I may also have found a solution how to do it without a loop (the loop takes time in my case). I converted the data to to 1 or 0 using "sign" function. The "search" function then finds the position of the first 1. For the last constant values, I do the same after I reverse the order of data and substract the constant value from the array. I, then, use a subarray function to get the result. The schematis is bolow. Looks like it works fine and fast. Any comments are appreciated.