Skip to content

Instantly share code, notes, and snippets.

@MpoMp
Created March 14, 2014 10:47
Show Gist options
  • Save MpoMp/9545561 to your computer and use it in GitHub Desktop.
Save MpoMp/9545561 to your computer and use it in GitHub Desktop.
Usage of inject on yale NMatrix
require 'nmatrix'
b = NMatrix.new([4, 3], [1, 2, 3, 4, 5, 6, 7, 7, 7, 8, 9, 10])
c = NMatrix.new([4, 3], stype: :yale, default: 0)
c[0,0] = 1
c[1,1] = 2
c[2,2] = 4
c[3,2] = 8
puts b
puts c
#try to get the sum of each column
column_sums_b = []
column_sums_c = []
b.cols.times do |i|
#puts "B column: #{b.col(i)}" # these columns are NMatrix objects
#puts "C column: #{c.col(i)}" # these columns are NMatrix objects as well
column_sums_b << b.col(i).inject(:+) # this will return a number
column_sums_c << c.col(i).inject(:+) # this will return concatenated arrays with some strange(?) numbers
end
#puts column_sums_b
#puts column_sums_c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment