From: Hugh Sasse Date: 2006-03-10T23:32:10+09:00 Subject: Re: how to introduce reference objects into ruby On Fri, 10 Mar 2006, Geert Fannes wrote: > But things change when you want to implement a SPARSE matrix, i.e., a > matrix for which most of its elements are empty. E.g., I am analyzing > call-networks, data from a telecom firm that contains the call durations > for its customers, which can be represented with a matrix: element (i,j) > is the duration that j calls i. Since not everybody calls to everybody, # Untested code. # There may well be a faster way, but you don't need pointers to do # this. class Durations def initialize() @matrix = Hash.new end def insert(i,j,duration) @matrix[[i,j]] = duration end def getrow(i) return getline(0,1,i) end def getcol(i) return getline(1,0,i) end private # get horizontal or vertical line def getline(a,b,i) line = [] linehash = {} @matrix.keys.each do |k| linehash[k[b]] = @matrix[k] if k[a] == i end linehash.keys.sort.each do |index| line << linehas[index] end return line end end # HTH # Hugh