Given two Fixnum arrays of equal length, is there a Ruby way to produce an array holding the maximum or minimum values at each index?
For example:
a1 = [ 10, 30 ]
a2 = [ 5, 35 ]
The min function would return 5 from the first column and 30 from the second column, giving [5, 30]. Likewise, the max function would return 10 from the first column and 35 from the second column, giving [10, 35].
You can construct arrays of the elements at common positions (that is, transpose a row-wise 2D array into a column-wise 2D array), then map the min or max (or whatever else) values out of each column:
[a1, a2].transpose.map &:min # => [5, 30]
[a1, a2].transpose.map &:max # => [10, 35]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With