代码之家  ›  专栏  ›  技术社区  ›  DollarChills

在循环中查找匹配项

  •  0
  • DollarChills  · 技术社区  · 7 年前

    我有一个循环,它获取一个用户输入的号码(没有代表),并在此基础上插入某些信息。

    例如,如果用户输入数字3,则将在r.u索引中插入一个介于1和3之间的数字。

    |id|treatment_selection_id|repetition_index|treatment_index|
    |1 |         1            |       1        |               |
    |2 |         1            |       2        |               |
    |3 |         1            |       3        |               |
    |4 |         2            |       1        |               |
    |5 |         2            |       2        |               |
    |6 |         2            |       3        |               |
    |7 |         3            |       1        |               |
    |8 |         3            |       2        |               |
    |9 |         3            |       3        |               |
    

    注意每个 treatment_selection_id 具有唯一的重复索引1..3。现在每 repetition_index 等于1,我想在 treatment_index

    到目前为止,我有以下几点,但是 r.treatment_index 不是为每个匹配插入唯一的号码吗 r.repetition_index .

    no_reps = @trial.number_of_repetitions
    @trial.treatment_selections.each do |r|
     r.repetitions.in_groups_of(no_reps).each_with_index do |a, i|
       treatment_indexes = (1..no_reps).to_a.shuffle
       a.each_with_index do |r, j|
         r.repetition_index = j + 1
         r.treatment_index = treatment_indexes[j]
       end
      end
    end
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   EmmanuelB    7 年前

    treatment_indexes 将被创建,所以您将随机分配它们在 treatment_selecion_id 水平。在你的结果中,你应该看到 治疗选择 数字相同的人会有不同的结果 treatment_index . 但是你找不到 repetition_index 治疗指数 .

    你可以创造这个 治疗指数 但是你仍然需要搜索 重复性指数 treatment_selection_id 和 具有相同的值,这将符合您所要求的唯一性行为。

    这种变化应该给你随机性,仍然消除了重复的 重复性指数

    no_reps = @trial.number_of_repetitions
    repet_treat = {} # you have to keep track of repetition_indexes and treatment_indexes
    @trial.treatment_selections.each do |r|
     r.repetitions.in_groups_of(no_reps).each_with_index do |a, i|
       treatment_indexes = (1..no_reps).to_a # randomness will be handled elsewhere
       a.each_with_index do |r, j|
         r.repetition_index = j + 1
         # store treatment_index for this repetition_index in an array
         repeat_treat[r.repetition_index] ||= [] 
         # get the treatment_index you have already used for this repetition_index
         used_treat_indexes = repeat_treat[r.repetition_index]
         # delete the used indexes from the posibilities of the next and get a new random index
         r.treatment_index = (treatment_indexes - used_treat_indexes).sample
         # store your newely used treatment_index in its repetition_index group
         repeat_treat[r.repetition_index] << r.treatment_index
       end
      end
    end
    

    结构 @trial 正在存储 治疗指数 @审判 而不是一个新的散列。

    no_reps = @trial.number_of_repetitions
    @trial.treatment_selections.each do |r|
     r.repetitions.in_groups_of(no_reps).each_with_index do |a, i|
       treatment_indexes = (1..no_reps).to_a # still handle randomness elsehwere
       a.each_with_index do |r, j|
         r.repetition_index = j + 1
         used_indexes = @trial
                          .treatment_selections
                          .map(&:repetitions)
                          .select { |rep| rep.repetition_index == r.repetition_index }
                          .map(&:treatment_index)
         r.treatment_index = (treatment_indexes - used_indexes).sample
       end
      end
    end