Transition Matrix that compares two elements to the next two elements.
显示 更早的评论
Hi all, I am trying to modify this code to create a transition matrix that compares two elements to the next two elements. As of now the code successfully compares two elements to the next one element. I have been trying to modify it, but have not had any luck. Any help would be greatly appreciated. Thanks.
close all
clc
dataset = [1 2 1 1 1 2 2 2 1 1 1 2 2];
precision = 1;
markovChain = (round(dataset/precision)).*precision;
%number of states
Nstates = max(markovChain);
%get Norder-contiguous sequences of the markov chain
ngrams = [];
for i = 0:1
ngrams = [ngrams, circshift(markovChain,[0 -1*(i)])'];
end
ngrams = cellstr(num2str( ngrams));
ngrams = ngrams(1:end-2);
%create all combinations of Norder-contiguous sequences
[x{1:2}] = ndgrid(1:Nstates);
%format x to cell
evalStr = ['xCell = cellstr(num2str(['];
for i = 1:2
evalStr = [evalStr 'x{' num2str(i) '}(:) '];
end
evalStr = [evalStr ']));'];
eval(evalStr);
%map ngrams to numbers
[gn,~,g]=unique([xCell;ngrams]);
s1 = g(Nstates^2+1:end);
%states following the ngrams
s2 = markovChain(3:end);
%get transition matrix
tm = full(sparse(s1,s2,1,Nstates^2,Nstates^2) );
transitionMatrix = bsxfun(@rdivide, tm, sum(tm,2));
9 个评论
Azzi Abdelmalek
2015-7-27
编辑:Azzi Abdelmalek
2015-7-27
Can you post the expected result. There may be a better solution then modifying your code
Azzi Abdelmalek
2015-7-27
where the element in the firs row firs col would [1 1]. What does that mean? Can you explain what do you mean by comparison
Ellie
2015-7-27
Azzi Abdelmalek
2015-7-27
For the first column of the result it's clear, what about the second one?
Ellie
2015-7-27
Walter Roberson
2015-7-27
Walter Roberson
2015-7-27
This is not good code :(
Walter Roberson
2015-7-28
I have given a solution in http://uk.mathworks.com/matlabcentral/answers/231381-error-using-accumarray-third-input-sz-must-be-a-full-row-vector-with-one-element-for-each-column-of to the form of the question presented there, which does not use those messy eval().
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Numeric Types 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!