# Need to find how many times in the matrix the Value 1 goes to Value 2

2 views (last 30 days)
Jesse Valentin on 21 Apr 2019
Edited: Jesse Valentin on 22 Apr 2019
I'm looking for a way to find the number of times a specific transition occurs between two values in a matrix A. Matrix A is 9x9 and has values ranging from 0 to 8. I need to find the number of times a value, say 5, is immediately followed by another value, say 2.
%This an example of matrix A
A=[1 3 4 0;4 2 3 1;1 2 3 1]
For this matrix, I would need to find multiple combinations
1 to 3
4 to 0
3 to 1
etc.
I have no clue where to go with this. Thanks for any help.
Edit: Right now, i have a 8x30 excel spreadsheet with data that will be entered into a matrix. I then need to find different combinations of entries, where one value follows another value, ie 0 to 0, 0 to 1,etc. this is just part of the data set, and the missing row will be filled in. So in the first row, 0 to 0 would result in an output of 1.

Walter Roberson on 22 Apr 2019
The [10 10] is not because of 10 columns: it is because of 10 different states, since I used randi([0 9]). The [10 10] is choosing the output array size. Every state needs the possibility to transition to every other state so if NS is number of states then use [NS NS] .
You could also just leave out the argument and let accumarray size the array for you. But suppose that the only time state 8 is seen is at the beginning of rows, that nothing ever transitions from a lower state to state 8. Then [something, 8]+1 never occurs in the input, and accumarray would not have any reason to create a column 8+1 in the output array. accumarray only creates a row or column if it or higher number appears in the input.
Remember to do the +1 by the way,
out = accumarray([A{1}(idx), A{1}(idx+1)] + 1, 1, [NS NS])
this is needed because you have 0 in the input, and the values in the first position must all be positive integers; so the data for 0 is in row/column 1, the data for 1 is in row/column 2, and so on.
The transition count from state N to state M would be at out(N+1,M+1)
Walter Roberson on 22 Apr 2019
Hint Hint: if you think about the index numbers more then you will find that you do not need to work row by row. Not every possible idx, idx+1 can happen, but the ones that cannot happen are easy to define and eliminate.
Jesse Valentin on 22 Apr 2019
so i am at this right now..
A{1}=[2 0 0 1 2 3 3 4 1 2 1 2 1 2 5 2 1 1 0 0 2 0 0 1 1 0 1 2 0 2];
idx = 1:30-1;
out=accumarray([A{1}(idx), A{1}(idx+1)] + 1,1, [10,10]);
Thanks for all the help, but don't worry about it. i'll just do the process by hand

Image Analyst on 21 Apr 2019
Here's one way:
% Define data
A{1} = [1 3 4 0]; % 4 elements (customers)
A{2} = [4 2 3 1 5 3 5 2 1 2]; % 10 elements (customers)
A{3} = [1 2 3 1 4 1 0 2]; % 8 elements (customers)
% Instantiate output large enough to handle all expected numbers.
output = zeros(60*24, 60*24); % 60*24 is the number of minutes in a day.
% Compute counts of each pair.
for k = 1 : length(A)
thisCell = A{k}
for col = 1 : length(thisCell) - 1
startValue = thisCell(col);
endValue = thisCell(col + 1);
% Increment value. Add 1 to row and column because counts for row 0 must go into row 1.
output(startValue + 1, endValue + 1) = output(startValue + 1, endValue + 1) + 1;
end
end
[rows, columns] = find(output);
% Crop to largest times
output = output(1 : max(rows), 1 : max(columns))