# How do I find a substring within two different strings at once?

1 回表示 (過去 30 日間)
David Haydock 2022 年 10 月 6 日
コメント済み: Les Beckham 2022 年 10 月 6 日
Lets say I have a Nx2 cell array:
X = {"Here is ABC", "Here is ABC";...
"Here is AFB", "Here is ABC"...
}
I need to check if both cells in the same row contain "ABC", and if they do, get the index of the row.
How can I do this in a succinct way? Any help would be appreciated.

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

### 採用された回答

Steven Lord 2022 年 10 月 6 日
If you're using string arrays, we recommend using a string array and not a cell array containing string arrays. If you do, you can use contains and the rest of the string processing functions. Once you have the logical array returned by the string processing functions use any, all, etc. on it.
X = {"Here is ABC", "Here is ABC";...
"Here is AFB", "Here is ABC"...
}
X = 2×2 cell array
{["Here is ABC"]} {["Here is ABC"]} {["Here is AFB"]} {["Here is ABC"]}
Y = string(X)
Y = 2×2 string array
"Here is ABC" "Here is ABC" "Here is AFB" "Here is ABC"
C = contains(Y, "ABC")
C = 2×2 logical array
1 1 0 1
E = endsWith(Y, "ABC")
E = 2×2 logical array
1 1 0 1
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Les Beckham 2022 年 10 月 6 日
So, to answer the original question, you would use find(all(C, 2)):
X = {"Here is ABC", "Here is ABC";...
"Here is AFB", "Here is ABC"...
};
Y = string(X);
C = contains(Y, "ABC")
C = 2×2 logical array
1 1 0 1
find(all(C,2)) % The all(C,2) means determine if all columns (dim 2) are true, find gets the index
ans = 1

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

### カテゴリ

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

### Community Treasure Hunt

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

Start Hunting!

Translated by