Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7031445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:48:15+00:00 2026-05-28T00:48:15+00:00

I have a two dimensional array, which represents columns and rows of data. I

  • 0

I have a two dimensional array, which represents columns and rows of data. I need to sum both the columns and rows, but I need to total from the the new ‘summary’ row.

Data (6×5 array)

[1, 0, 3, 0, 0], 
[0, 4, 0, 0, 4], 
[0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0]

so the result should be a 7×6 array

[1, 0, 3, 0, 0, 4], 
[0, 4, 0, 0, 4, 8], 
[0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0],
[1, 4, 3, 0, 4, 12]

I know I can sum each column and add an additional row to my two dimensional array via

# Sum the columns, add additional one row for summary
a << a.transpose.map{|x| x.reduce(:+)}

but how do I add the additional column

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:48:16+00:00Added an answer on May 28, 2026 at 12:48 am
    a.map! {|row| row + [row.reduce(:+)]}
    

    map! takes each element of the array, passes it to the block and replaces that element with whatever that block returns. So since we call it on a 2d array, row will be a 1d array – the row of the original array.

    Then I calculate the sum with reduce(:+) of that row. Then I need to append it to that row. What I’ve done here is to wrap the result of sum into an array and then used + to concatenate those two arrays.

    I could have also done this:

    a.map! {|row| row << row.reduce(:+) }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a quadratic matrix(two-dimensional dynamic array of pointers) and need to change rows/columns
I have a two-dimensional array (of Strings) which make up my data table (of
I have a two dimensional array that I need to load data into. I
I have a large number of data points which are two dimensional coordinates with
I have a output file which is a two dimensional array (this file was
wonder if anyone can help; I have an a two dimensional array which I
I have a two dimensional array, i.e. an array of sequences which are also
I have a variable called names which is a two dimensional array. I declared
I have a two dimensional array of points, which constitute a map. The black
I have a query which retrives two colums of multiple rows from the db.I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.