I can't choose just names!

1 Ansicht (letzte 30 Tage)
Depressive Mood
Depressive Mood am 14 Mai 2020
Kommentiert: Image Analyst am 14 Mai 2020
I have this question homework. And I can't find the result.And this is the question and the code which I wrote.
Assume that at each row of a 2D character array, a name exists between two asterisk signs (*). At each row, along with other characters, there is only one name and there are only two asterisk signs. A name can be anywhere in the row. Using Octave, write a function called "longest_name" that will accept an array with above-given properties as input and return the longest name in this array. Example: If Records = ['123*Ali*36' ; '*Veli*1783' ; '99*Zeynep*'] and Out = longest_name(Records), Out will be 'Zeynep'.
a = {'123*Ali*36' ; '*Veli*178' ; '99*Zeynep*'} ;
for k =1:length(a)
val(k)=size(a,1) ;
strfind=('*')
substr=(0, *)
end
out=a(val==max(val))

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 14 Mai 2020
Depressive Mood - since this his homework, we can only give out hints. You need to extract the name from between the two asterisks. You can do this with regular expressions but an easier way might be to use strsplit. How might using this function be helpful?
  3 Kommentare
Geoff Hayes
Geoff Hayes am 14 Mai 2020
You would need to use strfind as
indices = strfind(s, '*');
where s is your string that you want to find the indices of the asterisks. If indices is non-empty and has exactly two elements, then you know that everything between those two indices corresponds to the name.
a = {'123*Ali*36' ; '*Veli*178' ; '99*Zeynep*'} ;
for k =1:length(a)
s = a{k};
indices = strfind(s, '*');
% etc.
end
Image Analyst
Image Analyst am 14 Mai 2020
There may be some differences between MATLAB and Octave. I think you should ask in an Octave forum, or better yet, use MATLAB instead.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by