[RUBY] How to add the same Indexes in a nested array

Thing you want to do

numbers = [
 [1,2,3],
 [4,5,6],
 [7,8,9]
]

I was thinking of a method to get the following array by adding the same indexes from such an array.

sums = [12, 15, 18]

Conclusion

numbers = [[1,2,3], [4,5,6], [7,8,9]]
sums = numbers.transpose.map{ |num| num.sum }

is.

At first, I was thinking about the nested "how to get the 0th of the array", and I was worried about the efficient acquisition method. However, if you think of it as a matrix and transpose it, it was one shot.

Matrix transpose is possible with transpose in Ruby. Reference: instance method Array # transpose

Recommended Posts

How to add the same Indexes in a nested array
How to add a new hash / array
How to add sound in the app (swift)
How to add a classpath in Spring Boot
How to convert a file to a byte array in Java
How to add columns to a table
How to change a string in an array to a number in Ruby
How to create a placeholder part to use in the IN clause
How to retrieve the hash value in an array in Ruby
How to add elements without specifying the length of the array
How to add the delete function
How to make a Java array
How to output the value when there is an array in the array
How to store the information entered in textarea in a variable in the method
How to call multiple names at once in the same category
[Xcode] How to add a README.md file
How to insert a video in Rails
How to add jar file in ScalaIDE
How to get the date in java
How to publish a library in jCenter
How to get the current date as a string in yyyyMMdd format
How to change the value of a variable at a breakpoint in intelliJ
How to get the absolute path of a directory running in Java
[Rails] How to create a table, add a column, and change the column type
[Java] [For beginners] How to insert elements directly in a 2D array
How to reference a column when overriding the column name method in ActiveRecord
How to check the logs in the Docker container
How to display a web page in Java
How to create pagination for a "kaminari" array
How to create a theme in Liferay 7 / DXP
How to implement a like feature in Rails
Add classpath: to the path specified in spring.datasource.schema
How to make a follow function in Rails
How to build the simplest blockchain in Ruby
How to check Rails commands in the terminal
How to automatically generate a constructor in Eclipse
[Swift] How to get the number of elements in an array (super basic)
If the parameter is an array, how to include it in Stopara's params.permit
How to make a judgment method to search for an arbitrary character in an array
How to ZIP a JAVA CSV file and manage it in a Byte array
How to store data simultaneously in a model associated with a nested form (Rails 6.0.0)
How to make a unique combination of data in the rails intermediate table
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
How to create a Java environment in just 3 seconds
How to set the display time to Japan time in Rails
[Java] How to omit the private constructor in Lombok
How to run the SpringBoot app as a service
How to implement a like feature in Ajax in Rails
How to use an array for a TreeMap key
How to create a Spring Boot project in IntelliJ
Calculate the difference between numbers in a Ruby array
How to create a data URI (base64) in Java
Organized how to interact with the JDK in stages
How to launch another command in a Ruby program
How to display a browser preview in VS Code
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
How to store Rakuten API data in a table
How to convert A to a and a to A using AND and OR in Java
How to specify the resource path in HTML import
How to query Array in jsonb with Rails + postgres