sliding-window
How do I lag columns in MySQL?
Here is a solution that returns what you want in MySQL SET @a :=0; SET @b :=2; SELECT r.id, r.value, r.value/r2.value AS ‘lag’ FROM (SELECT if(@a, @a:=@a+1, @a:=1) as rownum, id, value FROM results) AS r LEFT JOIN (SELECT if(@b, @b:=@b+1, @b:=1) as rownum, id, value FROM results) AS r2 ON r.rownum = r2.rownum MySQL … Read more
How to use a timer in C++ to force input within a given time?
#include <iostream> #include <thread> #include <chrono> #include <mutex> #include <condition_variable> using namespace std; condition_variable cv; int value; void read_value() { cin >> value; cv.notify_one(); } int main() { cout << “Please enter the input: “; thread th(read_value); mutex mtx; unique_lock<mutex> lck(mtx); while (cv.wait_for(lck, chrono::seconds(2)) == cv_status::timeout) { cout << “\nTime-Out: 2 second:”; cout << “\nPlease … Read more
Does reactive extensions support rolling buffers?
This is possible by combining the built-in Window and Throttle methods of Observable. First, let’s solve the simpler problem where we ignore the maximum count condition: public static IObservable<IList<T>> BufferUntilInactive<T>(this IObservable<T> stream, TimeSpan delay) { var closes = stream.Throttle(delay); return stream.Window(() => closes).SelectMany(window => window.ToList()); } The powerful Window method did the heavy lifting. Now … Read more
Sliding window of M-by-N shape numpy.ndarray
You can do a vectorized sliding window in numpy using fancy indexing. >>> import numpy as np >>> a = np.array([[00,01], [10,11], [20,21], [30,31], [40,41], [50,51]]) >>> a array([[ 0, 1], [10, 11], [20, 21], #define our 2d numpy array [30, 31], [40, 41], [50, 51]]) >>> a = a.flatten() >>> a array([ 0, 1, … Read more
Sum values in a rolling/sliding window
What you have is a vector, not an array. You can use rollapply function from zoo package to get what you need. > x <- c(1, 2, 3, 10, 20, 30) > #library(zoo) > rollapply(x, 3, sum) [1] 6 15 33 60 Take a look at ?rollapply for further details on what rollapply does and … Read more
What is Sliding Window Algorithm? Examples?
I think of it as more a technique than an algorithm. It’s a technique that could be utilized in various algorithms. I think the technique is best understood with the following example. Imagine we have this array: [ 5, 7, 1, 4, 3, 6, 2, 9, 2 ] How would we find the largest sum … Read more
Simulate lag function in MySQL
This is my favorite MySQL hack. This is how you emulate the lag function: SET @quot=-1; select time,company,@quot lag_quote, @quot:=quote curr_quote from stocks order by company,time; lag_quote holds the value of previous row’s quote. For the first row @quot is -1. curr_quote holds the value of current row’s quote. Notes: order by clause is important … Read more