Skip to content

Instantly share code, notes, and snippets.

@jdevoo
Created September 2, 2015 11:24
Show Gist options
  • Save jdevoo/c3372666b4c65f1e6bd7 to your computer and use it in GitHub Desktop.
Save jdevoo/c3372666b4c65f1e6bd7 to your computer and use it in GitHub Desktop.
# FCM iteration function
# returns a table of (iterations x state vectors)
fcm.iterate = function(mat, init, peg, iter, trans, fn, ...) {
acc = matrix(0, iter, ncol(mat))
acc[1,] = init
if(!missing(peg)) {
acc[1,which(!is.na(peg))] = peg[!is.na(peg)]
}
for(i in 2:iter) {
if(trans) {
acc[i,] = sapply(t(acc[i-1,]) %*% mat, fn, ...)
} else {
acc[i,] = sapply(mat %*% acc[i-1,], fn, ...)
}
if(!missing(peg)) {
acc[i,which(!is.na(peg))] = peg[!is.na(peg)]
}
}
return(acc)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment