How to Combine Multiple Arrays of the Same Size in Ruby

How to combine multiple arrays of the same size in ruby

[a,b,c].transpose

is all you need. I prefer this to zip 50% of the time.

How to efficiently concatenate multiple arrays in Ruby?

If you've already determined that multiple concatenation is the fastest method, you can write it nicer using reduce:

[foo, bar, baz].reduce([], :concat)

Merge and interleave two arrays in Ruby

You can do that with:

a.zip(s).flatten.compact

How to join multiple arrays in ruby which has equal size

Something like this should do the trick:

chars.map.with_index {|char, i| 
prices[i].zip(names[i], numbers[i], pics[i], [char].cycle)
}.flatten(1)

Merge multiple arrays

Not certain what the question is, but assuming that the array given is arr, you can get the result by:

arr
.group_by(&:first)
.map{|k, v| [k, *v.transpose.drop(1).map{|a| a.inject(:+)}]}

or as pointed out by Cary Swoveland:

arr
.group_by(&:first)
.map{|k, v| [k, *v.transpose.drop(1).map(&:sum)]}

Result:

[
["Horse", 8, 7, 12, 0],
["Dog", 1, 3, 3, 0],
["Test", 5, 0, 2, 2],
["Cat", 5, 2, 4, 0]
]

Ruby: How to concatenate array of arrays into one

You're looking for #flatten:

concatenated = array_of_arrays.flatten

By default, this will flatten the lists recursively. #flatten accepts an optional argument to limit the recursion depth – the documentation lists examples to illustrate the difference.

Ruby, equally distribute elements and interleave/merge multiple arrays

I would use a sort to do this, based on element index postion, divided by size of array, plus some offset based on array id, to keep things consistent (if you don't need consistency, you could use a small random offset instead).

a = [:a,:b]
b = [:c]
c = [:d,:e,:f]
d = [:g:,:h,:i,:j]

def sort_pos array, id
(1..array.size).map { |i| (i - 0.5 + id/1000.0)/(array.size + 1e-6) }
end

# Combine all the arrays with their sort index, assigning ids to each array for consistency.
# Depending on how you receive these arrays, this structure can be built up programatically,
# as long as you add an array plus its sort index numbers at the same time
combined = (a + b + c + d).zip( sort_pos(a, 1) + sort_pos(b, 2) + sort_pos(c, 3) + sort_pos(d, 4) )

# Extract the values from the original arrays in their new order
combined.sort_by { |zipped| zipped[1] }.map { |zipped| zipped[0] }

=> [:g, :d, :a, :h, :e, :i, :b, :f, :j, :c]

There might be a cleaner way of doing this in Ruby . . . but I think the end result is what you are after - an "even" mix of multiple arrays.

If you only care about even-ness of mix from a statistical perspective (i.e. over time it is "fair"), you could just do this:

(a+b+c+d).shuffle

=> [:g, :b, :i, :c, :a, :h, :e, :j, :f, :d]

How do I combine two arrays based on the index of their elements?

From the docs:

If the size of any argument is less than the size of the initial array, nil values are supplied.

So you do not need to worry about the case where the second array is shorter.

If the first array is shorter, pad it out with nil to the length of the second array.

You can use compact to remove the superfluous nils afterwards.

Case 1: a is longer

a = [1, 2, 3, 4, 5]
b = [6, 7, 8]

a[b.count-1] ||= nil
a.zip(b).map(&:flatten).map(&:compact)

Result:

[[1, 6], [2, 7], [3, 8], [4], [5]]

Case 2: b is longer

a = [1, 2, 3]
b = [4, 5, 6, 7, 8]

a[b.count-1] ||= nil
a.zip(b).map(&:flatten).map(&:compact)

Result:

[[1, 4], [2, 5], [3, 6], [7], [8]]   

Variation with nil included

a = [1, 2, 3, 4, 5]
b = [6, 7, 8]

a[b.count-1] ||= nil
b[a.count-1] ||= nil
a.zip(b).map(&:flatten) # [[1, 6], [2, 7], [3, 8], [4, nil], [5, nil]]

... and ...

a = [1, 2, 3]
b = [4, 5, 6, 7, 8]

a[b.count-1] ||= nil
b[a.count-1] ||= nil
a.zip(b).map(&:flatten) # [[1, 4], [2, 5], [3, 6], [nil, 7], [nil, 8]]

Notes

  • If a or b must not be modified, insert a .clone somewhere to clone them beforehand.
  • The .flatten was taken from the OP's example; it flattens any arrays that take the role of the integers in the example. Keep or leave off as needed.

How to merge arrays to one array kind of respectively in ruby

a = [1,2,3,4,5] 
b = [6,7,8,9,10]
c = [11,12,13,14,15]

((a.zip b).zip c).flatten.compact
=> [1, 6, 11, 2, 7, 12, 3, 8, 13, 4, 9, 14, 5, 10, 15]


Related Topics



Leave a reply



Submit