滑动窗口算法在C# [英] Sliding window algorithm in C#

查看:340
本文介绍了滑动窗口算法在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现简单的滑动窗口alogirithm二维数组在C#3.0, 我发现<一href="http://distilledb.com/blog/archives/date/2009/03/12/enumerable-extension-method-for-contiguous-subsequences.page"相对=nofollow>这个中非常有用的,但它仅涉及单一dimensioal阵列。

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.

<一个href="http://distilledb.com/blog/archives/date/2009/03/12/enumerable-extension-method-for-contiguous-subsequences.page"相对=nofollow>的帖子还包括code的算法中,我完全以没有用它为我塞纳里奥......任何人可以建议我我怎么着手?

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?

方案:

上图是10X10矩阵,并需要获得3X3矩阵出它,使用任何算法(滑动窗口将是马丽娟)。红色矩形为第一组和绿色的是第二个。它的推移,直到最后所有行

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:我用Google搜索有关的算法中,但没有运气:(

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

推荐答案

幼稚的:

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方法,二维数组之间复制:

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主:

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();
}

输出:

(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 :)

这篇关于滑动窗口算法在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆