determining the sum of top-left to bottom-right diagonal values in a matrix with Ruby? -


i have square matrix of indeterminate row & column length (assume rows , columns equal befits square).

i've plotted out example matrix follows:

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

my goal sum top-left bottom-right of diagonal values.

obviously in example, i'll need:

diagsum = matrix[0][0]+matrix[1][1]+matrix[2][2] #=>  15 

i see pattern it's +1 incremental each row & column argument in matrix, code i've developed matrix of indeterminate length (supplied argument method diagsum preferably need implement sort of row_count method on matrix argument.

if

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

then:

require 'matrix' matrix[*arr].trace   #=> 15 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -