在两个向量之间交换元素(交叉)

编程入门 行业动态 更新时间:2024-10-27 07:20:27
本文介绍了在两个向量之间交换元素(交叉)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有:

chromosome_1 <- c('0010000001010000')

chromosome_2 <- c('0100000001001010')

如何实现 第3-5步 ?

How can I implement step 3-5 ?

  • 评估
    • NC1 =否. chromosome_1
    • 中1的个
    • NC2 =否. chromosome_2
    • 中1的个
    • M = min(NC1, NC2)
    • Evaluate
      • NC1 = no. of 1's in chromosome_1
      • NC2 = no. of 1's in chromosome_2
      • M = min(NC1, NC2)
      • 从基因中随机选择NC个基因位置 与来自chromosome_1的等位基因"1"并形成索引为s1的集合 这样的职位.

        Randomly select NC gene positions among the genes with allele "1" from chromosome_1 and form a set s1 of indices of such selected positions.

        从基因中随机选择NC个基因位置 来自chromosome_2的等位基因"1"并形成索引为s2的一组 这样的职位.

        Randomly select NC gene positions among the genes with allele "1" from chromosome_2 and form a set s2 of indices of such selected positions.

        s = union(s1, s2) 假设 s = 2, 3, 10, 15

        对于s

        在以下位置交换染色体chromosome_1​​和chromosome_2的等位基因 基因位置i.

        Exchange the alleles of chromosomes chromosome_1 and chromosome_2 at gene position i.

        以下说明了结果:

        真的很感谢您的帮助!

        推荐答案

        可能不是最简单的解决方案,但它可行

        Might not be the simplest solution, but it works

        set.seed(12345) ## Step 1 a <- c(0,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0) b <- c(0,1,0,0,0,0,0,0,0,1,0,0,1,0,1,0) m <- min(sum(a==1), sum(b==1)) ## Step 2 random_int <- sample(1:m, 1) ## Step 3 random_a <- sample(which(a == 1), random_int) random_b <- sample(which(b == 1), random_int) #all <- sort(union(random_a, random_b)) ## Step 4 ## for demo purpose (assume it as the random output) all <- c(2,3,10,15) temp_a <- a[all] temp_b <- b[all] ## Step 5 ##crossover b[all] <- temp_a a[all] <- temp_b ## Output > a [1] 0 1 0 0 0 0 0 0 0 1 0 1 0 0 1 0 > b [1] 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0
  • 更多推荐

    在两个向量之间交换元素(交叉)

    本文发布于:2023-06-11 17:31:24,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/639807.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:向量   元素   两个

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!