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
Post a Comment