[英]Find largest element from multidimensional array of ints
I have an array of ints similar to this: 我有一个类似这样的整数数组:
values = [[3, 4, 15, 16, 5, 13, 2], [1, 12, 13, 2, 10, -1], [11, 12, 1, 9, -2], [1, -10, -2, -13], [-11, -3, -14], [8, -3], [-11]]
How would I retrieve the index of the largest number, eg 16 == values[0][3]
? 如何检索最大数量的索引,例如
16 == values[0][3]
?
Don't sure I get you correct, but anyway: 不确定我是否正确,但是无论如何:
1) If you need the largest element: 1)如果您需要最大的元素:
values.flatten.max
2) If you need largest element from each subarray: 2)如果您需要每个子数组中的最大元素:
values.map{|x| x.max}
UPD UPD
About indexes: 关于索引:
largest_element = values.flatten.max
values.each_with_index do |e,i|
if e.include?(largest_element)
p i
values[i].each_with_index{|e, i| p i if e == largest_el}
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.