distribution of consecutive numbers

조회 수: 2 (최근 30일)
Danielle Leblance
Danielle Leblance 2018년 10월 30일
댓글: John D'Errico 2018년 10월 30일
Hi ,
I have a column of 9 states (attached the file) . How can I know in this column how many times each state is followed by another state? in other words, the values ranges between 1 and 9. how can i know how many times 1 is followed by 1, 1 followed by 2, ...1 followed by 9 (the same goes for each state)?

채택된 답변

Guillaume
Guillaume 2018년 10월 30일
편집: Guillaume 2018년 10월 30일
I don't think there's a way t do that without a loop (either explicit or with arrayfun).
%build list of patterns to search:
[n1, n2] = ndgrid(1:9);
patterns = [n1(:), n2(:)];
%search for each pattern with a loop:
x1 = reshape(x1, 1, []); %need a row vector
count = zeros(size(patterns, 1), 1);
for row = 1:size(patterns, 1)
count(row) = numel(strfind(x1, patterns(row, :))); %despite its name strfind can be used to find patterns of numbers
end
%pretty display
table(patterns, count)
edit: actually I was completely wrong, it can easily be done without a loop, but temporarily requires much more memory:
[n1, n2] = ndgrid(1:9);
patterns = [n1(:), n2(:)];
%count of pattern
transitions = permute([x1(1:end-1), x1(2:end)], [3 2 1]);
count = sum(all(patterns == transitions, 2), 3)
%pretty display
table(patterns, count)
edit again: Another option is to build the graph of the transitions and use the edgecount function:
g = graph(x1(1:end-1), x1(2:end));
[n1, n2] = ndgrid(1:9);
count = edgecount(g, n1(:), n2(:));
patterns = [n1(:), n2(:)];
table(patterns, count)

추가 답변 (2개)

John D'Errico
John D'Errico 2018년 10월 30일
편집: John D'Errico 2018년 10월 30일
Actually, easy. No loop needed, and just one line of code. You are essentially looking to find an array, I'll call it Counts, such that Counts(i,j) is the number of times the number i was followed by the number j in the vector x1. Pretty much two lines of code.
Counts = accumarray([x1(1:end-1),x1(2:end)],ones(numel(x1)-1,1))
Counts =
25 5 2 3 0 0 0 0 0
4 29 5 1 3 0 0 0 0
5 3 33 0 0 3 0 0 1
1 0 0 25 1 3 11 1 0
0 4 0 5 32 6 0 3 0
0 1 5 2 6 29 1 0 3
0 0 0 5 2 0 36 6 6
0 0 0 1 7 0 4 24 2
0 0 0 0 0 6 3 4 28
So, a 1 was followed by a 1 exactly 25 times, but a 3 was followed by 9 EXACTLY once. The most common occurrence was the pair [7 7], which we saw 36 times. We can test quickly to verify that fact.
strfind(x1',[7 7])
ans =
Columns 1 through 31
14 15 62 68 69 70 71 72 81 95 96 101 102 121 122 201 213 286 287 288 289 290 291 292 308 309 310 311 312 330 331
Columns 32 through 36
332 333 338 339 347
Yep. 36 occasions where [7 7] was seen. Where did that lonely [3 9] pair arise?
strfind(x1',[3 9])
ans =
33
If the vector x1 included 0 or negative numbers, we would need a second line, utilizing the function unique.
  댓글 수: 2
Image Analyst
Image Analyst 2018년 10월 30일
Very nice and clever.
I think graycomatrix() could also do the same thing since this is it's reason for being - what the function was made for.
John D'Errico
John D'Errico 2018년 10월 30일
Yeah, I don't have the IPT, but it has many utilities that can be applied to non-imaging problems, so that I could arguably justify the IPT even if I never did image processing.
The only problem with the above code is if the vector included zeros or negatives. But then you just use unique first on it.
x = round(rand(1000,1)*8 - 4);
[xu,~,xrefs] = unique(x);
xu
xu =
-4
-3
-2
-1
0
1
2
3
4
Counts = accumarray([xrefs(1:end-1), ...
xrefs(2:end)],ones(numel(xrefs)-1,1))
Counts =
2 8 4 6 9 6 6 11 2
6 30 28 14 8 22 15 18 9
9 17 11 20 18 15 16 13 1
5 19 15 10 18 23 12 14 6
7 22 10 21 15 13 18 17 9
12 16 17 15 22 15 20 7 7
5 17 17 19 20 16 19 11 6
4 14 11 11 11 15 21 12 9
4 7 7 6 11 6 3 5 3

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


madhan ravi
madhan ravi 2018년 10월 30일
편집: madhan ravi 2018년 10월 30일
load matlab.mat
u = unique(x1)
Expected_result = histc(x1,u)
consecutive_numbers_and_their_repetition = [u Expected_result]
  댓글 수: 2
Guillaume
Guillaume 2018년 10월 30일
Danielle Leblance comment mistakenly posted as an answer moved here:
? unique shows unique values and not how many times 1 is followed by 2 ,etc..
madhan ravi
madhan ravi 2018년 10월 30일
check edited answer

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by