Find following values and represent them in a matrix

1 回表示 (過去 30 日間)
elisa ewin
elisa ewin 2016 年 9 月 12 日
編集済み: Stephen23 2016 年 9 月 12 日
Hi! I have a matrix
A=[23 34 45 0 0 0;21 34 0 0 23 11;34 23 0 0 0 22;23 11 21 0 0 45;11 45 23 0 0 0]
I have found the unique values in the matrix:
U = unique(A) = [0 11 21 22 23 34 45]
Excluding the value 0, I want a matrix 6x6 (6 is the number of values found without zero) in which I want to represent the number of times that a value is following from another value in every row.
Eg
Occurence that after 11, there is 11 is zero
Occurence that after 11, there is 21 is 1
Occurence that after 11, there is 22 is zero
Occurence that after 11, there is 23 is zero
Occurence that after 11, there is 34 is zero
Occurence that after 11, there is 45 is 1
So the first row of the matrix I want is:
B = [0 1 0 0 0 1]
Occurence that after 21, there is 11 is zero
Occurence that after 21, there is 21 is 0
Occurence that after 21, there is 22 is 0
Occurence that after 21, there is 23 is 0
Occurence that after 21, there is 34 is 1
Occurence that after 21, there is 45 is 1
So the second row of the matrix is
B = [0 1 0 0 0 1; 0 0 0 0 1 1; ...]
I want to repeat the same process, for all the values in U.
I have no idea, I can do it
Can you help me? thanks
  1 件のコメント
Stephen23
Stephen23 2016 年 9 月 12 日
Are you sure that "Occurence that after 21, there is 45 is 1" ?
I don't see any occurrences of 45 after 21, anywhere in the matrix A.

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

採用された回答

Stephen23
Stephen23 2016 年 9 月 12 日
編集済み: Stephen23 2016 年 9 月 12 日
A = [23,34,45,0,0,0;21,34,0,0,23,11;34,23,0,0,0,22;23,11,21,0,0,45;11,45,23,0,0,0];
[R,C] = ndgrid(unique(A(A~=0)));
fun = @(r,c)nnz( r==A(:,1:end-1) & c==A(:,2:end));
out = arrayfun(fun,R,C)
creates this:
out =
0 1 0 0 0 1
0 0 0 0 1 0
0 0 0 0 0 0
2 0 0 0 1 0
0 0 0 1 0 1
0 0 0 1 0 0

その他の回答 (0 件)

カテゴリ

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