Find following values and represent them in a matrix

1 visualizzazione (ultimi 30 giorni)
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 Commento
Stephen23
Stephen23 il 12 Set 2016
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.

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 12 Set 2016
Modificato: Stephen23 il 12 Set 2016
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

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by