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

1 view (last 30 days)
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.
Screen Shot 2019-04-21 at 12.48.06 PM.png
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.
  12 Comments
Walter Roberson
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
Jesse Valentin on 22 Apr 2019
Edited: 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

Sign in to comment.

Answers (1)

Image Analyst
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))

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!