Need to find how many times in the matrix the Value 1 goes to Value 2
1 回表示 (過去 30 日間)
古いコメントを表示
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.
12 件のコメント
Walter Roberson
2019 年 4 月 22 日
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.
回答 (1 件)
Image Analyst
2019 年 4 月 21 日
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))
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!