How to find pattern in an array?
92 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2017 年 12 月 5 日
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
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 件のコメント
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!