How can I find if there is at least one particular element in the string?

1 view (last 30 days)
Hello,
s =(1111,1112,1121,1211,2111,2112,2121,2211,2122,2112,2212,2212,2222,1122,1221,1222)
I want to know if there is any way to know which of the elemnts contains only one '1'?

Answers (1)

Ameer Hamza
Ameer Hamza on 24 Oct 2020
Edited: Ameer Hamza on 24 Oct 2020
You can convert it to a string and then find how many contains a single 1
x = [1111,1112,1121,1211,2111,2112,2121,2211,2122,2112,2212,2212,2222,1122,1221,1222];
idx = cellfun(@numel, strfind(string(x), '1'))==1;
y = x(idx);
Result
>> y
y =
2122 2212 2212 1222

Categories

Find more on Cell Arrays in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!