how to find patterns in an array

조회 수: 3 (최근 30일)
Riley
Riley 2014년 10월 3일
답변: Riley 2014년 10월 3일
Hello
I have a 19*19 array filled with either a 1, -1 or a 0.
I need to find patterns of 5 repeating values next to each other of any value in both row and columns, and then store where the patterns are in an array.
example:
[1 0 0 0 0 0 1 0 -1 ]
and then store where the patterns are in another 19*19 array. As either a row pattern, column pattern or both.
any suggestions on how to do it? I'm completely lost.
  댓글 수: 1
per isakson
per isakson 2014년 10월 3일
편집: per isakson 2014년 10월 3일
Search the FEX for run length encoding.

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

채택된 답변

Guillaume
Guillaume 2014년 10월 3일
A loop is probably the simplest and clearest way to do it:
A = randi(3, 20) - 2; %for demo
patternlength = 5;
pl = patternlength - 1; %actually more useful than patternlength
positions = zeros(size(A)) %output matrix
for col = 1:size(A, 2) - pl
for row = 1:size(A, 1)- pl
if all(diff(A(row:row+pl, col)) == 0) || all(diff(A(row, col:col+pl)) == 0)
positions(row, col) = 1;
end
end
end

추가 답변 (2개)

Andrei Bobrov
Andrei Bobrov 2014년 10월 3일
n = 5;
idxv = find(conv2(double(diff(A) == 0),ones(n-1,1),'same') == n-1);
idxv = idxv - 1;
idxh = find(conv2(double(diff(A,1,2) == 0),ones(1,n-1),'same') == n-1);
idxh = idxh - size(A,1);

Riley
Riley 2014년 10월 3일
Thankyou very much Guillaume, that worked perfectly. Very much appreciated.
Andrei thank you for your time as well.

카테고리

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

태그

아직 태그를 입력하지 않았습니다.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by