Common substring index from two string arrays

13 ビュー (過去 30 日間)
Preetham Manjunatha
Preetham Manjunatha 2021 年 11 月 2 日
コメント済み: Stephen23 2021 年 11 月 2 日
I have two string arrays a and b and I need to find the index matching the substring numbers (within string arrays). For example, in a string array a, 01, 02, 03 are the substrings matching in string array b.
a = {'frame01', 'frame02', 'frame03'};
a = string(a);
b = {'capture00.jpg' 'capture01.jpg', 'capture02.jpg', 'capture03.jpg', 'capture04.jpg'};
b = string(b)
Desired output index of string array b should be:
Index_b = [2 3 4]
Desired output index of string array a should be:
Index_a = [1 2 3]
Without for-loop solution is preferred.
Any help is appreciated!
  4 件のコメント
Stephen23
Stephen23 2021 年 11 月 2 日
Do the substring numbers require the same leading zeros or just the same numeric value, i.e. is '3' the same as '03' ?
Preetham Manjunatha
Preetham Manjunatha 2021 年 11 月 2 日
It is numbers with leading zeros in this example. But in general can be without leading zeros.

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

採用された回答

Stephen23
Stephen23 2021 年 11 月 2 日
a = ["frame01", "frame02", "frame03"];
b = ["capture00.jpg", "capture01.jpg", "capture02.jpg", "capture03.jpg", "capture04.jpg"];
[~,ia,ib] = intersect(regexp(a,'\d+','once','match'),regexp(b,'\d+','once','match'))
ia = 3×1
1 2 3
ib = 3×1
2 3 4
  2 件のコメント
Preetham Manjunatha
Preetham Manjunatha 2021 年 11 月 2 日
Thanks!
Stephen23
Stephen23 2021 年 11 月 2 日
And if you want to compare numeric values (i.e. disregard the leading zeros) then:
a = ["frame01", "frame02", "frame03"];
b = ["capture00.jpg", "capture01.jpg", "capture02.jpg", "capture03.jpg", "capture04.jpg"];
[~,ia,ib] = intersect(...
str2double(regexp(a,'\d+','once','match')),...
str2double(regexp(b,'\d+','once','match')))
ia = 3×1
1 2 3
ib = 3×1
2 3 4

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by