"Andrew " <atto.42+matlab@gmail.com> wrote in message <i1d4mr$kvs$1@fred.mathworks.com>...
> I've been bumbling around trying to learn Matlab, and am trying to compare data in a large vector with the next row in that vector (the possible values are integers, 1 to 8 inclusive). Specifically, I want to create a square matrix showing the probabilities of each value being followed by the next value.
>
> So far, my hackish solution was to make a crosstabulation between the vector, and a copy of the vector with an offset of 1.
>
> >> crosstab(Data,DataCopy)
>
> ans =
>
> 34 66 38 99 4 58 53 8
> 70 626 808 538 12 309 177 19
> 70 780 1209 490 6 291 127 13
> 75 576 555 369 10 257 137 22
> 9 3 3 18 2 10 6 3
> 63 296 244 315 11 168 117 18
> 34 192 119 149 7 119 70 13
> 6 20 9 23 2 20 16 9
>
> Unfortunately, I'd like the same thing, but with proportions in each cell. For example, in column 1 (summation of the column), there's 361 occurrences, so I'd want the cells in column 1 to be divided by 361. Is there an easy way to do this?
>
> Thanks for your help.
          
Just apply matlab's sum function to the columns of your answer and then use bsxfun with division to divide the columns elementbyelement by those sums. Or else do an appropriate repmat on the sums and do elementbyelement division.
I assume that you used data(1:end1) and data(2:end) for the two arguments of crosstab. By dividing by the sums of the columns you would be producing the conditional probabilities, given that x2 has the jth value, it is preceded by the ith value in x1. However the way you worded your request, "the probabilities of each value being followed by the next value", sounded as if you wanted the conditional probabilities the other way around. If so, you would have to divide by the row sums instead of the column sums.
Roger Stafford
