0

私はRubyプログラミングの初心者であり、次の質問に対する答えを見つけることができません。配列列arr[]。friends全体の標準偏差を計算する単一のコマンドはありますか?

#!/usr/bin/ruby
require 'descriptive_statistics'
class MyList < Struct.new(:id, :name,:friends)
end
arr = Array.new
f = File.open("test-comma.txt", "r")
f.each_line { |line| words = line.split(',')
   p = MyList.new
   p.id=words[0]
   p.name=words[1]
   p.friends=words[2]
   arr.push(p)
   }
f.close

私がインターネットで見つけることができるすべての例は、1次元配列(ベクトル)を提供します。また、gem "descriptive_statistics"には事前に作成された統計関数があるため、 injectを使用しても意味がありません。

4

1 に答える 1

0

descriptive_statisticsEnumerableあなたができるように拡張します:

arr.collect{|p| p.friends}.standard_deviation

これはもちろん、p.friendsが番号である場合に提供されます

于 2012-09-24T20:12:12.230 に答える