如何创建逻辑向量的排列?

时间:2019-01-15 08:34:51

标签: r

是否有一个函数可以帮助我输出长度为n的布尔向量的所有2 ^ n个排列?例如,如果我有一个长度为n = 2的布尔向量c(FALSE,FALSE),则我应该获得2 ^ 2 = 4个排列。

这样,我需要一个函数,该函数将把该输出推广为长度为n的数组, 这意味着如果n = 3,则输出的长度应为2 ^ 3,依此类推...

我已经尝试过gtools包中的排列,但这似乎是不正确的,或者至少可以提供部分答案。这种方法不能很好地泛化,并且给了我n> 2的错误。

> permutations(2,2,c(TRUE,FALSE))
      [,1]  [,2]
[1,] FALSE  TRUE
[2,]  TRUE FALSE

输出应为:

FALSE, FALSE,
TRUE, TRUE,
FALSE, TRUE,
TRUE, FALSE

3 个答案:

答案 0 :(得分:1)

您可以使用expand.grid

expand.grid(c(TRUE, FALSE), c(TRUE, FALSE))
#   Var1  Var2
#1  TRUE  TRUE
#2 FALSE  TRUE
#3  TRUE FALSE
#4 FALSE FALSE

答案 1 :(得分:1)

您想念[DisallowConcurrentExecution] public class TestJob : IJob { private ILoggingService Logger { get; } private IApplicationLifetime ApplicationLifetime { get; } private static object lockHandle = new object(); private static bool shouldExit = false; public TestJob(ILoggingService loggingService, IApplicationLifetime applicationLifetime) { Logger = loggingService; ApplicationLifetime = applicationLifetime; } public Task Execute(IJobExecutionContext context) { return Task.Run(() => { ApplicationLifetime.ApplicationStopping.Register(() => { lock (lockHandle) { shouldExit = true; } }); try { for (int i = 0; i < 10; i ++) { lock (lockHandle) { if (shouldExit) { Logger.LogDebug($"TestJob detected that application is shutting down - exiting"); break; } } Logger.LogDebug($"TestJob ran step {i+1}"); Thread.Sleep(3000); } } catch (Exception exc) { Logger.LogError(exc, "An error occurred during execution of scheduled job"); } }); } } 的地方:

private void ConfigureQuartz(IServiceCollection services, params Type[] jobs)
{
    services.AddSingleton<IJobFactory, QuartzJobFactory>();
    services.Add(jobs.Select(jobType => new ServiceDescriptor(jobType, jobType, ServiceLifetime.Singleton)));

    services.AddSingleton(provider =>
    {
        var schedulerFactory = new StdSchedulerFactory();
        var scheduler = schedulerFactory.GetScheduler().Result;
        scheduler.JobFactory = provider.GetService<IJobFactory>();
        scheduler.Start();
        return scheduler;
    });
}

protected void ConfigureJobsIoc(IServiceCollection services)
{
    ConfigureQuartz(services, typeof(TestJob), /* other jobs come here */);
}

public void ConfigureServices(IServiceCollection services)
{
    ConfigureJobsIoc(services);

    // other stuff comes here
    AddDbContext(services);
    AddCors(services);

    services
        .AddMvc()
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}


protected void StartJobs(IApplicationBuilder app, IApplicationLifetime lifetime)
{
    var scheduler = app.ApplicationServices.GetService<IScheduler>();
    //TODO: use some config
    QuartzServicesUtilities.StartJob<TestJob>(scheduler, TimeSpan.FromSeconds(60));

    lifetime.ApplicationStarted.Register(() => scheduler.Start());
    lifetime.ApplicationStopping.Register(() => scheduler.Shutdown());
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory,
    ILoggingService logger, IApplicationLifetime lifetime)
{
    StartJobs(app, lifetime);

    // other stuff here
}

您可以围绕repeats.allowed=T进行自定义功能:

gtools::permutations(2,2, c(T,F), repeats.allowed = T)

      [,1]  [,2]
[1,] FALSE FALSE
[2,] FALSE  TRUE
[3,]  TRUE FALSE
[4,]  TRUE  TRUE

包含更多元素的示例:

permutations

答案 2 :(得分:0)

您可以使用 gtools 包和 permutations 函数: 这是源代码:

M. Deinum