Find the index of the first integer of an array in another array

2 ビュー (過去 30 日間)
Mike Mierlo van
Mike Mierlo van 2020 年 5 月 1 日
コメント済み: dpb 2020 年 5 月 3 日
Hi,
I want to find the index of the first integer of an array in another array. For example.
I have array A = [ 0 0 0 ] and I have array B= [ 1 2 3 4 0 0 0 5 6 7 0 0 8 9 ]
A = [ 0 0 0 ];
B= [ 1 2 3 4 0 0 0 5 6 7 0 0 8 9 ];
I want find the index of array B where array A starts. In this case array A starts from index 5 in array B.
So my outcome should be:
ans = 5
The thing I am looking for looks like find() but instead of finding just one value, I am searching for multiple values in succession of each other.

採用された回答

dpb
dpb 2020 年 5 月 1 日
Easier to find repeated patterns with string pattern searches than as vector arrays--
ix=strfind(sprintf('%d',ismember(B,A)),repmat('1',1,numel(A)));
  2 件のコメント
Tommy
Tommy 2020 年 5 月 1 日
Using strfind is brilliant! To expand on this, if A happens to contain different values:
ix=strfind(sprintf('%d',B),sprintf('%d',A));
dpb
dpb 2020 年 5 月 3 日
Actually that's a nice refinement -- no real need to convert to logical.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by