matlab 转移矩阵,在Matlab中构造多阶马尔可夫链转移矩阵
6個(gè)狀態(tài)的一階轉(zhuǎn)移矩陣可以是
constructed very elegantly as
x = [1 6 1 6 4 4 4 3 1 2 2 3 4 5 4 5 2 6 2 6 2 6]; % the Markov chain
tm = full(sparse(x(1:end-1),x(2:end),1)) % the transition matrix.
所以這是我的問(wèn)題,你如何優(yōu)雅地構(gòu)建二階轉(zhuǎn)移矩陣?
我想出的解決方案如下
[si sj] = ndgrid(1:6);
s2 = [si(:) sj(:)]; % combinations for 2 contiguous states
tm2 = zeros([numel(si),6]); % initialize transition matrix
for i = 3:numel(x) % construct transition matrix
tm2(strmatch(num2str(x(i-2:i-1)),num2str(s2)),x(i))=...
tm2(strmatch(num2str(x(i-2:i-1)),num2str(s2)),x(i))+1;
end
是否有單/雙線,無(wú)環(huán)替代?
–
編輯:
我嘗試將我對(duì)Amro的解決方案與“x = round(5 * rand([1,1000])1)進(jìn)行比較;”
% ted teng's solution
Elapsed time is 2.225573 seconds.
% Amro's solution
Elapsed time is 0.042369 seconds.
有什么區(qū)別!
僅供參考,grp2idx可在線獲取.
總結(jié)
以上是生活随笔為你收集整理的matlab 转移矩阵,在Matlab中构造多阶马尔可夫链转移矩阵的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: windows下python画图matp
- 下一篇: 苹果手机扬声器没声音怎么办_手机扬声器盲