Seeking assistance creating a frequency matrix

2 ビュー (過去 30 日間)
John
John 2013 年 1 月 3 日
Hello,
I was hoping that somebody might be able to help me out in creating a frequency matrix?
I normally use excel for statistical modelling but this particular problem takes hours to execute using spreadsheets.
I have created a variables called 'data' and it contains numbers ranging from 0-828.
I was hoping to create a frequency matrix of the number of times one number follows another number. Or put a different way, the number of times a number transitions to another number.
Here is a simple example with numbers ranging from 1-5.
For example, the cell in red, the number 3 is followed by 2 twice, or transitions to 2 twice.
I'd appreciate any help that is offered.
Kind Regards
John

採用された回答

Walter Roberson
Walter Roberson 2013 年 1 月 3 日
dcol = data(:); %need column vector form
freqmatrix = accumarray( [dcol(1:end-1) + 1, dcol(2:end) + 1], 1 );
This would create a matrix up to 829 x 829. Any one position, (R,C) in it, would indicate a transition from (R-1) to (C-1). The offset of 1 is needed because you have 0 values and subscripts cannot be 0.
If the number of unique values is considerably smaller than 829, then it would be possible to make the matrix much smaller, at the expense of making it less natural to read.
  3 件のコメント
John
John 2013 年 1 月 3 日
Thanks you Walter, this does exactly what I wanted. Have a good day.
Roger Stafford
Roger Stafford 2013 年 1 月 3 日
If your data values are not positive integers or are not closely-packed near zero, you can do a 'unique' call first. To use Walter's nomenclature,
[ud,~,nd] = unique(data(:));
N = length(nd);
freqmatrix = accumarray([nd(1:N-1),nd(2:N)],1,[N,N]);
The rows and columns in N x N 'freqmatrix' would then correspond to values in 'ud'.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2013 年 1 月 3 日
John, though you don't know it (yet), what you are describing is called the "gray level co-occurence matrix". This is done by graycomatrix() in the Image Processing Toolbox.
  7 件のコメント
John
John 2013 年 1 月 3 日
Hello Roger,
Yes, that is how I got the transition matrix.
Are you saying to simualte 20 steps in a markov chain that I take the transition matrix to the power of 20?
Would you have time to demonstrate this? I new to matlab and don't quite know how to do this, or if I follow you correctly?
Kind Regards
Roger Stafford
Roger Stafford 2013 年 1 月 3 日
Yes, T^20. Let's suppose you have the transition matrix
T = [ .3 , .7 ;
.6 , .4 ]
Then T^2 = T*T would be
T^2 = [ .3*.3+.7*.6 , .3*.7+.7*.4 ;
.6*.3+.4*.6 , .6*.7+.4*.4 ]
= [ .51 , .49 ;
.42 , .58 ]
In other words, for example, given that you have started with 1 the probability that after two steps you will end with 2 (.49) is the probability that after one step you will remain at 1 (.3) times the conditional probability that on the next step you will go to 2 (.7) plus the probability that on the first step you will go to 2 (.7) times the conditional probability that on the next step you will remain at 2 (.4) giving a total of .3*.7+.7*.4 = .49 . This is just what matrix multiplication accomplishes.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by