根据区间获取权重的所有排列

发布于 2024-11-26 17:43:17 字数 553 浏览 2 评论 0原文

如果我有给定数量的“系统”

var nbSystems = 2;

和多个“步骤”,

var nbSteps = 3;

我如何生成以下所有排列:

{0.00, 0.00}
{0.33, 0.00}
{0.67, 0.00}
{1.00, 0.00}
{0.00, 0.33}
{0.33, 0.33}
{0.67, 0.33}
{1.00, 0.33}
{0.00, 0.66}
{0.33, 0.66}
{0.67, 0.66}
{1.00, 0.66}
{0.00, 1.00}
{0.33, 1.00}
{0.67, 1.00}
{1.00, 1.00}

我需要每个系统的权重在 0 到 1 之间,并且我想找到所有排列。

您会看到用于系统权重的间隔是根据 nbSteps 计算得出的(间隔 = 1 / nbSteps)。

我想要一个函数,它接受输入 nbSystems 和 nbSteps (或更好?),并且将输出某种具有所有排列的多维数组......

If I have a given number of 'systems'

var nbSystems = 2;

and a number of 'steps'

var nbSteps = 3;

How can I generate all the following permutations :

{0.00, 0.00}
{0.33, 0.00}
{0.67, 0.00}
{1.00, 0.00}
{0.00, 0.33}
{0.33, 0.33}
{0.67, 0.33}
{1.00, 0.33}
{0.00, 0.66}
{0.33, 0.66}
{0.67, 0.66}
{1.00, 0.66}
{0.00, 1.00}
{0.33, 1.00}
{0.67, 1.00}
{1.00, 1.00}

I need a weight between 0 to 1 for each system and I want to find all permutations.

You see that the interval to use for the systems weights is calculated from the nbSteps (interval = 1 / nbSteps).

I would like to have a function that takes inputs nbSystems and nbSteps (or better?) and that will output some sort of multidimensional array with all the permutations...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枯寂 2024-12-03 17:43:17

这应该可以做到:

static void Main(string[] args)
{
    var nbSystems = 2;
    var nbSteps = 3;

    var steps = GetSteps(0, 1, nbSteps).Select(n => Math.Round(n, 2)).ToArray();
    foreach (var seq in GetCombinations(steps, nbSystems))
        Console.WriteLine(string.Join(", ", seq));
}

private static IEnumerable<decimal> GetSteps(decimal min, decimal max, int count)
{
    var increment = (max - min) / count;
    return Enumerable.Range(0, count + 1).Select(n => min + increment * n);
}

private static IEnumerable<IEnumerable<T>> GetCombinations<T>(
    ICollection<T> choices, int length)
{
    if (length == 0)
    {
        yield return new T[0];
        yield break;
    }

    foreach (var choice in choices)
        foreach (var suffix in GetCombinations(choices, length - 1))
            yield return Enumerable.Concat(new[] { choice }, suffix);
}

如果您关心组合的顺序,请根据需要进行调整。

编辑:哎呀,修正了一个拼写错误 - 我在类型签名中调换了“max”和“min”。

This should do it:

static void Main(string[] args)
{
    var nbSystems = 2;
    var nbSteps = 3;

    var steps = GetSteps(0, 1, nbSteps).Select(n => Math.Round(n, 2)).ToArray();
    foreach (var seq in GetCombinations(steps, nbSystems))
        Console.WriteLine(string.Join(", ", seq));
}

private static IEnumerable<decimal> GetSteps(decimal min, decimal max, int count)
{
    var increment = (max - min) / count;
    return Enumerable.Range(0, count + 1).Select(n => min + increment * n);
}

private static IEnumerable<IEnumerable<T>> GetCombinations<T>(
    ICollection<T> choices, int length)
{
    if (length == 0)
    {
        yield return new T[0];
        yield break;
    }

    foreach (var choice in choices)
        foreach (var suffix in GetCombinations(choices, length - 1))
            yield return Enumerable.Concat(new[] { choice }, suffix);
}

Adjust as necessary if you care about the order of the combinations.

EDIT: Oops, fixed a typo - I transposed "max" and "min" in my type signature.

痴者 2024-12-03 17:43:17

尝试

double nbSteps = 3.0;

IEnumerable<double> SystemA = Enumerable.Range (0, (int)nbSteps).Select (x => x / nbSteps); 
IEnumerable<double> SystemB = Enumerable.Range (0, (int)nbSteps).Select (x => x / nbSteps); 

var Result = from a in SystemA from b in SystemB select new { a, b };

try

double nbSteps = 3.0;

IEnumerable<double> SystemA = Enumerable.Range (0, (int)nbSteps).Select (x => x / nbSteps); 
IEnumerable<double> SystemB = Enumerable.Range (0, (int)nbSteps).Select (x => x / nbSteps); 

var Result = from a in SystemA from b in SystemB select new { a, b };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文