필터 지우기
필터 지우기

How to find pattern in an array?

조회 수: 93 (최근 30일)
MathWorks Support Team
MathWorks Support Team 2017년 12월 5일
댓글: gwoo 2022년 5월 25일
How to find a pattern from a numeric array?
For example, there is a numeric array B and pattern (or mask) A. How to get the pattern location found in B? 
 
>> A = [1 2 3];
>> B = [5,4,3,1,2,3,5,4,1,2,3,4,5];
 
Expected output: 4, 9
 

채택된 답변

MathWorks Support Team
MathWorks Support Team 2017년 12월 5일
There is no built-in MATLAB function that performs the exact operation described.
However, you can use a single for-loop and the built-in "all" and "find" functions to create a custom function that will output the desired behavior:
>> function output = pattern(B, A)
>> SIZE = length(B) - length(A);
>> match = zeros(1, SIZE);
>> for i=1:SIZE
>> match(i) = all(B(i:i-1+length(A)) == A);
>> end
>> output = find(match == 1);
>> end
  댓글 수: 1
gwoo
gwoo 2022년 5월 25일
As @claudio points out below in More Answers, strfind(B, A) does exactly what is requested.

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

추가 답변 (1개)

claudio
claudio 2020년 5월 14일
output = strfind(B,A);
  댓글 수: 1
claudio
claudio 2021년 1월 14일
No exceptions. if you have a particular request you can submit it to evaluate the specific case
str = sprintf('Special string for \t Karel \t K');
pattern = sprintf('\t');
idxTab = strfind(str,pattern)
idxTab =
20 28

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

카테고리

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

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by