MATLAB: Calculating the probability of one bin preceding the other

MATLABmatrix

Hi,
I'm having a difficult time with this problem.
A matrix, M (2500×2), was binned into B:
bins = 1:1:8
B =
2 4
1 1
1 1
1 3
1 1
8 8
7 8
I'd like to know the probability of [1,1] proceeding [8,8].
The only thought I have is the obvious: counting each instance of [1,1] where [8,8] comes right before it, and then divide by 2500. However simple that is, I'm unable to implement it.
Any help would be greatly appreciated. Thank you, Scott

Best Answer

Counting can be done using ismember
tf11 = ismember(A,[1 1],'rows') ; % true for rows that are [1 1]
N11 = sum(tf11) % count

q = [false ; tf11(1:end-1)] ; % true for rows following a row with [1 1]
tf = ismember(A(q,:),[8 8],'rows') ; % true when these rows are [8 8]
N11followedby88 = sum(tf) % count