Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Cross-tabulation with proportions?

Subject: Cross-tabulation with proportions?

From: Andrew

Date: 11 Jul, 2010 19:06:03

Message: 1 of 2

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 cross-tabulation 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.

Subject: Cross-tabulation with proportions?

From: Roger Stafford

Date: 12 Jul, 2010 01:48:05

Message: 2 of 2

"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 cross-tabulation 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 element-by-element by those sums. Or else do an appropriate repmat on the sums and do element-by-element division.

  I assume that you used data(1:end-1) 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 j-th value, it is preceded by the i-th 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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us