# Forum: Rails-ES Matriz dispersa

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
on 2013-11-19 22:47
```Class SparseVector

def initialize(h = {})
@vector = Hash.new(0)
@vector = @vector.merge!(h)
end

def [](i)
@vector[i]
end

def to_s
@vector.to_s
end
end

class SparseMatrix

def initialize(h = {})
@matrix = Hash.new({})
for k in h.keys do
@matrix[k] = if h[k].is_a? SparseVector
h[k]
else
@matrix[k] = SparseVector.new(h[k])
end
end
end

def [](i)
@matrix[i]
end

def col(j)
c = {}
for r in @matrix.keys do
c[r] = @matrix[r].vector[j] if
@matrix[r].vector.keys.include? j
end
SparseVector.new c
end
end

##Llamo a la Clase SparseMatrix pasandole un vector para el Hash###
hsh = SparseMatrix.new 1 => {1 => 33, 2 => 44, 3 => 55}, 2 => {1 => 66,
3 => 77}
puts hsh[1][3]
puts hsh.col(2)

Este es el código que tengo,necesito saber si puedo acceder a los
índices del hash de alguna manera recorriendolo mediante un bucle.
He probado mediante un each do y no lo he conseguido hacer.
Necesito realizar esto para realizar operaciones entre matrices densas y
matrices dispersas.```
This topic is locked and can not be replied to.