Find following values and represent them in a matrix
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!