Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Convert the following python code into MATLAB thank you! transitionTable = {'sunny': {'sunny': 0.8, 'cloudy': 0.2, 'rainy': 0}, 'cloudy': {'sunny': 0.4, 'cloudy': 0.4, 'rainy': 0.2},
Convert the following python code into MATLAB thank you! transitionTable = {'sunny': {'sunny': 0.8, 'cloudy': 0.2, 'rainy': 0}, 'cloudy': {'sunny': 0.4, 'cloudy': 0.4, 'rainy': 0.2}, 'rainy': {'sunny': 0.2, 'cloudy': 0.6, 'rainy': 0.2}} #print (transitionTable['sunny']['cloudy'])
import random print (list(transitionTable.keys())) #rand = random.choice(transitionTable.keys())
import random for i in range(5): rand = random.choice(list(transitionTable.keys())) print('Given data today is ', rand) print('The probabilities for tomorrow wheather are') for val in transitionTable[rand]: print(val, transitionTable[rand][val])
import numpy as np tab = np.matrix('0.8 0.2 0; 0.4 0.4 0.4; 0.2 0.6 0.2') print(tab)
import random today = 'rainy' print(today) #prob = random.uniform(0,1) for i in range(5): nxtDay = random.choice(list(transitionTable[today].keys())) print('Day ', i+1,':', nxtDay) today = nxtDay
import random today = 'rainy' T = np.transpose(np.matrix('0 0 1')) print(today) final = np.transpose(np.matrix('0. 0. 0.')) for i in range(1000): # tirar vies da escolha de um estado inicial prob = random.uniform(0,1) nxtDay = np.transpose(tab) * T k = 0 argnxt = -1 #print (nxtDay) for pos, j in enumerate(nxtDay): k += j if (prob < k): argnxt = pos break #print('prob:', prob, argnxt) nxtDay.fill(0) nxtDay[argnxt] = 1 T = nxtDay
import numpy as np tab = np.matrix('0.8 0.2 0; 0.4 0.4 0.2; 0.2 0.6 0.2') print(tab)
horizonte = 500000 for i in range(horizonte): prob = random.uniform(0,1) nxtDay = np.transpose(tab) * T k = 0 argnxt = -1 #print (nxtDay) for pos, j in enumerate(nxtDay): k += j if (prob < k): argnxt = pos break nxtDay.fill(0) nxtDay[argnxt] = 1 final += nxtDay T = nxtDay #print(final) final /= horizonte print(final, np.sum(final))
import random today = 'sunny' T = np.transpose(np.matrix('1 0 0')) print(today) final = np.transpose(np.matrix('0. 0. 0.')) for i in range(1000): # tirar vies da escolha de um estado inicial prob = random.uniform(0,1) nxtDay = np.transpose(tab) * T k = 0 argnxt = -1 #print (nxtDay) for pos, j in enumerate(nxtDay): k += j if (prob < k): argnxt = pos break nxtDay.fill(0) nxtDay[argnxt] = 1 T = nxtDay horizonte = 500000 for i in range(horizonte): prob = random.uniform(0,1) nxtDay = np.transpose(tab) * T k = 0 argnxt = -1 #print (nxtDay) for pos, j in enumerate(nxtDay): k += j if (prob < k): argnxt = pos break nxtDay.fill(0) nxtDay[argnxt] = 1 #print('prob:', prob, argnxt) # for pos, _ in enumerate(nxtDay): # if (pos == argnxt): # nxtDay[pos] = 1. # else: # nxtDay[pos] = 0. #print('--------------------') final += nxtDay T = nxtDay #print(final) final /= horizonte print(final, np.sum(final))
statDist = np.linalg.solve(np.matrix('-.2 .4 .2; .2 -.6 .6; 1 1 1'), np.matrix('0; 0; 1')) # np.linalg.solve(a,b) # Solve a linear matrix equation, or system of linear scalar equations. # Computes the exact solution, x, of the well-determined, i.e., full rank, linear matrix equation ax = b. print(statDist)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started