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!