Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Markovketten
- # Funktion
- markov.fun <- function(n = 2, M){
- text1 <- "\nn muss eine nateurliche Zahl groesser gleich 0 sein!"
- text2 <- "\nM muss eine quadratische Matrix sein!"
- # n muss eine natuerliche Zahl >= 0 sein
- if(!is.numeric(n) || n < 0 || !(n %% 1 == 0)) {
- stop(text1)
- }
- # M muss eine quadratische Matrix sein
- if(ncol(M) != nrow(M) || !(is.matrix(M))){
- stop(text2)
- }
- # bei n = 0
- if(n == 0){
- return(diag(ncol(M)))
- }
- # bei n > 0
- if(n > 0){
- i <- 0
- while(i < n){
- res <- M %*% M
- i <- i + 1
- }
- }
- return(res)
- }
- # Rekursion
- markov.rek <- function(n = 2, M){
- text1 <- "\nn muss eine nateurliche Zahl groesser gleich 0 sein!"
- text2 <- "\nM muss eine quadratische Matrix sein!"
- if(!is.numeric(n) || n < 0 || !(n %% 1 == 0)) {
- stop(text1)
- }
- if(ncol(M) != nrow(M) || !(is.matrix(M))){
- stop(text2)
- }
- if(n == 0){
- return(diag(ncol(M)))
- }
- M %*% markov.rek(n-1, M)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement