Function to create duplicate obs based on the value of a string variable

2 ビュー (過去 30 日間)
phlie
phlie 2016 年 8 月 15 日
回答済み: Star Strider 2016 年 8 月 15 日
In my dataset, I have a variable which takes values like abc-ABC or abc-def-ABC (i.e., one or more lower case codes and one upper case code). I would like to (1) count the number of lower case codes and capture this no. in a new variable (2) multiply the initial observation by this number (e.g., for abc-def-ABC, I would want 2 obs). Can anyone help?

回答 (2 件)

Star Strider
Star Strider 2016 年 8 月 15 日
This works:
str = {'abc-ABC'; 'abc-def-ABC'};
r = regexp(str, '[a-z]{3}-');
Result = cellfun(@numel, r, 'UniformOutput',false);

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 15 日
str='abc-def-keh-ABC'
a=regexp(str,'[a-z]+','match')
out1=numel(a)

カテゴリ

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