Great tutorial! I’ll throw my few pennies to this tutorial for Markov chains. There are always other ways to do the same thing.
Instead of matrix, you can also use different data structures to represent the Markov chain. For example hash:
h = {
0=>[0,1,2,3,1],
1=>[2,3,4,5,3,2],
2=>[2,0,1,5],
3=>[1,3,4,6],
4=>[2,0,2],
5=>[3,2,5],
6=>[2,3,1]
}
degree = 0
live_loop :markov do
play scale(:E, :major)[degree]
degree = h[degree].choose
sleep 0.25
end
Dont mind the chain - its prob awful to listen to but i hope it proves the point. I usually like this approach because it’s easier to follow which degree might go to which and so on. The chain contains essentially the same information as the Markov matrix.
Don’t believe me? Try it yourself
def chain_to_matrix(h)
h.values.map do |o|
arr = o.inject(Array.new(h.length, 0.0)) {|r,v| r[v]+=1.0; r}
arr.map{|v| v/=arr.inject(0, :+)}
end
end
print chain_to_matrix h