Find a specific array within a larger array

Hi all,
I am struggling with a problem. I have a large array, for example a = [11 12 13 14 15 16 13 14], and I want to find the index for a specfic sub sequence eg. b = [13 14 15]. So in this example I would want the index of 3 to indicate the position that b started in a. I dont want to know the indices of a that any number within b, i instead need them to be specifically in the order b, ie I dont care about the last two 13 14 numbers in a as it isnt the entirety of b.
I know I could simply do a loop that checks b against a subset of a and loops throughout the whole of a but this seems a little ineffectient if a is very big, (in my acutal code a is many 100,000's of numbers in length).
Is there a more efficent way, any help is appreiacted.
Thanks
J

2 件のコメント

Dyuman Joshi
Dyuman Joshi 2023 年 11 月 28 日
移動済み: Dyuman Joshi 2023 年 11 月 28 日
a = [11 12 13 14 15 16 13 14]
a = 1×8
11 12 13 14 15 16 13 14
b = [13 14 15]
b = 1×3
13 14 15
strfind(a, b)
ans = 3
Dyuman Joshi
Dyuman Joshi 2023 年 11 月 28 日
移動済み: Dyuman Joshi 2023 年 11 月 28 日
Ah, it seems that I'm just a little late to the party.

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

 採用された回答

james ingham
james ingham 2023 年 11 月 28 日

0 投票

Sorted it! strfind does excatly what I need. I didnt inital consider this as I wasnt handling strings but it turns out that doesnt matter.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

2023 年 11 月 28 日

移動済み:

2023 年 11 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by