http://www.mathworks.com/matlabcentral/newsreader/view_thread/286632
MATLAB Central Newsreader  Crosstabulation with proportions?
Feed for thread: Crosstabulation with proportions?
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Sun, 11 Jul 2010 19:06:03 +0000
Crosstabulation with proportions?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/286632#761677
Andrew
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.<br>
<br>
So far, my hackish solution was to make a crosstabulation between the vector, and a copy of the vector with an offset of 1.<br>
<br>
>> crosstab(Data,DataCopy)<br>
<br>
ans =<br>
<br>
34 66 38 99 4 58 53 8<br>
70 626 808 538 12 309 177 19<br>
70 780 1209 490 6 291 127 13<br>
75 576 555 369 10 257 137 22<br>
9 3 3 18 2 10 6 3<br>
63 296 244 315 11 168 117 18<br>
34 192 119 149 7 119 70 13<br>
6 20 9 23 2 20 16 9<br>
<br>
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?<br>
<br>
Thanks for your help.

Mon, 12 Jul 2010 01:48:05 +0000
Re: Crosstabulation with proportions?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/286632#761701
Roger Stafford
"Andrew " <atto.42+matlab@gmail.com> wrote in message <i1d4mr$kvs$1@fred.mathworks.com>...<br>
> 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.<br>
> <br>
> So far, my hackish solution was to make a crosstabulation between the vector, and a copy of the vector with an offset of 1.<br>
> <br>
> >> crosstab(Data,DataCopy)<br>
> <br>
> ans =<br>
> <br>
> 34 66 38 99 4 58 53 8<br>
> 70 626 808 538 12 309 177 19<br>
> 70 780 1209 490 6 291 127 13<br>
> 75 576 555 369 10 257 137 22<br>
> 9 3 3 18 2 10 6 3<br>
> 63 296 244 315 11 168 117 18<br>
> 34 192 119 149 7 119 70 13<br>
> 6 20 9 23 2 20 16 9<br>
> <br>
> 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?<br>
> <br>
> Thanks for your help.<br>
          <br>
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.<br>
<br>
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.<br>
<br>
Roger Stafford