2

I'm trying to implement simple sliding window alogirithm on two-dimensional array in C# 3.0, I found this as very useful but it involves only single-dimensioal array.

The post also includes the code for the algo, I'm totaly failed to use it for my senario... can any one suggest me how do I proceed?

Scenario:

http://parasu516.googlepages.com/matrix.jpg
(source: googlepages.com)

The above image is 10X10 matrix and need get 3X3 matrix out it, using any algorithm (Sliding window would be greate). Red rectangle is a first set and green one is the second. and it goes on till the end for all rows

PS: I googled about the algo, but no luck :(

5
  • Can you elaborate what is yours scenario? There are lots and lots of sliding window uses. Commented Jul 27, 2009 at 8:27
  • I included image representation for more clarity Commented Jul 27, 2009 at 8:47
  • what happens when your window hits the end of the x axes? Commented Jul 27, 2009 at 8:51
  • If I consider single row and window size as 3, we should stop at 7th index. Only 8 set will be returned Commented Jul 27, 2009 at 9:12
  • Links in question are dead Commented Jun 24, 2016 at 18:51

1 Answer 1

4

Naive implementation:

private static IEnumerable<T[,]> GetWindows<T>(
    T[,] array,
    int windowWidth,
    int windowHeight)
{
    for (var y = 0; y < array.GetLength(1) - windowHeight + 1; y++)
    {
        for (var x = 0; x < array.GetLength(0) - windowWidth + 1; x++)
        {
            var slice = new T[windowWidth, windowHeight];
            CopyArray(array, x, y, slice, 0, 0, windowWidth, windowHeight);
            yield return slice;
        }
    }
}

Helper method to copy between two-dimensional arrays:

private static void CopyArray<T>(
    T[,] src, int srcX, int srcY,
    T[,] dst, int dstX, int dstY,
    int width, int height)
{
    for (var x = 0; x < width; x++)
    {
        for (var y = 0; y < height; y++)
        {
            dst[dstX + x, dstY + y] = src[srcX + x, srcY + y];
        }
    }
}

Test main:

private static void Main(string[] args)
{
    var array = new string[5, 5];
    for (var y = 0; y < array.GetLength(1); y++)
    {
        for (var x = 0; x < array.GetLength(0); x++)
        {
            array[x, y] = string.Format("({0}|{1})", x, y);
        }
    }
    foreach (var window in GetWindows(array, 3, 3))
    {
        ShowArray(window);
    }
    Console.ReadLine();
}

private static void ShowArray<T>(T[,] array)
{
    for (var x = 0; x < array.GetLength(0); x++)
    {
        for (var y = 0; y < array.GetLength(1); y++)
        {
            Console.Write("    {0}", array[x, y]);
        }
        Console.WriteLine();
    }
    Console.WriteLine();
}

Output:

(0|0)    (0|1)    (0|2)
(1|0)    (1|1)    (1|2)
(2|0)    (2|1)    (2|2)

(1|0)    (1|1)    (1|2)
(2|0)    (2|1)    (2|2)
(3|0)    (3|1)    (3|2)

(2|0)    (2|1)    (2|2)
(3|0)    (3|1)    (3|2)
(4|0)    (4|1)    (4|2)

(0|1)    (0|2)    (0|3)
(1|1)    (1|2)    (1|3)
(2|1)    (2|2)    (2|3)

(1|1)    (1|2)    (1|3)
(2|1)    (2|2)    (2|3)
(3|1)    (3|2)    (3|3)

(2|1)    (2|2)    (2|3)
(3|1)    (3|2)    (3|3)
(4|1)    (4|2)    (4|3)

(0|2)    (0|3)    (0|4)
(1|2)    (1|3)    (1|4)
(2|2)    (2|3)    (2|4)

(1|2)    (1|3)    (1|4)
(2|2)    (2|3)    (2|4)
(3|2)    (3|3)    (3|4)

(2|2)    (2|3)    (2|4)
(3|2)    (3|3)    (3|4)
(4|2)    (4|3)    (4|4)

Now all you have to do is apply the same technique as shown in that blog post :)

Sign up to request clarification or add additional context in comments.

2 Comments

It take almost a minute to process 512X512 matrix with 13 windowSize, is there any scope for improvement
CopyArray is very expensive if done repeatedly. A simple optimization would be to yield return the x/y pair for each window instead of the window itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.