[Edited to correct sum(p, 1) to sum(p, 2).]
I think you submitted a similar question yesterday, but it seems to have been deleted. (I saw it, but the forum gave me an error when I tried to log on).
Warning: I know virtually nothing of Markov chains and transition matrices (so naturally, I'm going to try to address the question)...
You'd posted the example data:
d = [3 3 4 4 4 5 6 5 5 4];
and I think you want a matrix p of the probability that a given entry is followed by a given value.
[Note: if you want to use strfind like you did yesterday, I think you need to look at the help doc: it returns a list of of the first index of the start of the substring, so you'd want to use numel on the result to find how many substrings there were, not sum (which would sum the values of the indices, not their number).]
It makes more sense to me to use the additional output from the unique function that you used previously, then follow the method described here. d = [3 3 4 4 4 5 6 5 5 4];
[c, ~, ic] = unique(d);
N = numel(c);
p = zeros(N, N);
for k = 1:(numel(d) - 1)
p(ic(k), ic(k + 1)) = p(ic(k), ic(k + 1)) + 1;
end
p = bsxfun(@rdivide, p, sum(p, 2));
The result is
p =
0.5000 0.5000 0 0
0 0.6667 0.3333 0
0 0.3333 0.3333 0.3333
0 0 1.0000 0
I interpret the first row to mean: given a 3, there is a 50% chance the next value will be another 3, a 50% chance the next value will be a 4, and 0% chance of anything else. I interpret the second row to mean: given a 4, there is a 67% chance the next value will be another 4, a 33% chance the next value will be a 5, and a 0% chance of anything else. An so on... (Is this what you want?)
If you really insist on using strfind (and assuming the above procedure computed the correct result), I think the following should be equivalent (but far less efficient since strfind will cycle through the data N^2 times, instead of only once), and is closer to what you started with yesterday:
d = [3 3 4 4 4 5 6 5 5 4];
u1 = unique(d);
N = numel(u1);
x = zeros(N, 1);
y = zeros(N, N);
for i = 1:N
for j = 1:N
y(i, j) = numel(strfind(d, [u1(i), u1(j)]));
end
x(i) = sum(d(1:end - 1) == u1(i));
end
p = bsxfun(@rdivide, y, x);
Please accept this answer if it helps, or let me know in the comments if it completely misses the mark...
--Andy