2 views (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.

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.

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

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 12 Comments

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696450

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696450

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696454

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696454

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696463

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696463

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696492

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696492

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696495

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696495

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696496

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696496

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696629

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696629

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696631

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696631

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696634

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696634

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696645

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696645

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696647

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696647

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696823

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/457660-need-to-find-how-many-times-in-the-matrix-the-value-1-goes-to-value-2#comment_696823

Sign in to comment.