フィルターのクリア

How to find longest sequennce within a character array?

1 回表示 (過去 30 日間)
That Guy
That Guy 2020 年 11 月 16 日
コメント済み: Image Analyst 2020 年 11 月 16 日
lets say Im given a random character array such as: 'aaabee' and i want to find the longest sequence of vowels. How would i go about doing this?
so far I have:
cArr = 'aaabee';
vow = ['a', 'e', 'i', 'o', 'u'];
ans = strfind(cArr, vow);
but this just returns: [ ]
anyhelp would be appreciated!

回答 (2 件)

Ameer Hamza
Ameer Hamza 2020 年 11 月 16 日
編集済み: Ameer Hamza 2020 年 11 月 16 日
You can use regexp
cArr = 'xzyhd';
vow = '([aeiou]+)';
tkns = regexp(cArr, vow, 'tokens');
if isempty(tkns)
n = 0;
else
n = max(cellfun(@numel, [tkns{:}]));
end
Result
>> n
n =
5
  3 件のコメント
That Guy
That Guy 2020 年 11 月 16 日
編集済み: That Guy 2020 年 11 月 16 日
this works, i just need it to output zero when a string without vowels is entered such as 'xzyhd' or ' ' .
sorry im so helpless this is my first experince with coding:(
Ameer Hamza
Ameer Hamza 2020 年 11 月 16 日
I have updated the answer. Now it will output 0 for the mentioned cases.

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


Setsuna Yuuki.
Setsuna Yuuki. 2020 年 11 月 16 日
編集済み: Setsuna Yuuki. 2020 年 11 月 16 日
cArr = 'aaabee';
vow = ['a', 'e', 'i', 'o', 'u'];
for n=1:length(vow)
a = strfind(cArr, vow(n)); %search letter by letter
repeat{n} = a; %place in "cArr"
if (a ~= NaN)
long(n) = length(a); %number of repetitions
else
long(n) = 0;
end
end
t = table(vow',long')
Table:
  4 件のコメント
Setsuna Yuuki.
Setsuna Yuuki. 2020 年 11 月 16 日
編集済み: Setsuna Yuuki. 2020 年 11 月 16 日
has been interesting this problem, i changed all code because I understood bad your problem.
clear large;
cArr = 'aaeeaaapriioaeaaa';
vow = ['a', 'e', 'i', 'o', 'u'];
var = 1;
for n=1:length(vow)
a = strfind(cArr, vow(n));
if(var == 1)
var(length(var):(length(var)+length(a))-1)=a;
else
var(length(var)+1:length(var)+length(a))=a;
end
end
var = sort(var); m = 0;
cont = 1;
for n = 1:length(var)-1
if(var(n) == (var(n+1)-1) && n ~= length(var)-1)
cont = cont +1;
elseif (var(n) ~= (var(n+1)-1))
m = m+1;
large(m) = cont;
cont = 1;
elseif(n == (length(var)-1))
cont = cont+1;
m = m+1;
large(m) = cont;
end
end
[longSeq, index] = max(large);
fprintf("The longest sequence is %i and is the sequence %i \n",longSeq, index);
I think I did this in C ++ jaja xd
Image Analyst
Image Analyst 2020 年 11 月 16 日
What's the use case for this quirky thing? Why do you need to do it? It's not your homework you're asking people to do for you, is it? What's the real world use for this?

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by