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

조회 수: 1 (최근 30일)
Jesse Valentin
Jesse Valentin 2019년 4월 21일
편집: Jesse Valentin 2019년 4월 22일
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
Walter Roberson
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.
Jesse Valentin
Jesse Valentin 2019년 4월 22일
편집: Jesse Valentin 2019년 4월 22일
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

댓글을 달려면 로그인하십시오.

답변 (1개)

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

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by