Borrar filtros
Borrar filtros

How can i write 2D longest character array ?

1 visualización (últimos 30 días)
Oblique
Oblique el 16 de Mayo de 2020
Comentada: Image Analyst el 16 de Mayo de 2020
This array must be between two asterisk signs (*) and the code should give us the longest name in this array
For example: {'878*jhon*23 ; '*jonathan*87' ; '485*will*421'}
output will be as a 'jonathan'
I wrote code like this. but it's not what want.
a = {'23*Ali*36' ; 'Veli*178' ; '99*Zeynep*'} ;
for k=1:length(a)
val(k)=length(a{k});
x = findstr(a) , '*' )
end
out=a(val==max(val)

Respuesta aceptada

Image Analyst
Image Analyst el 16 de Mayo de 2020
Editada: Image Analyst el 16 de Mayo de 2020
Did you just try a super obvious brute force approach using a for loop:
a = {'23*Ali*36' ; 'Veli*178' ; '99*Zeynep*'} ;
nameLengths = zeros(length(a), 3); % Preallocate.
for k = 1 : length(a)
thisCellContents = a{k};
starLocations = find(thisCellContents == '*') % Find indexes of stars.
% Extract the name
if length(starLocations) >= 2
% Extract length between start #1 and star #2.
nameLengths(k, :) = [starLocations(1), starLocations(2), starLocations(2) - starLocations(1) + 1];
elseif length(starLocations) >= 1
% One star. Start from the beginning.
nameLengths(k, :) = [1, starLocations(1), starLocations(1)];
else
% No stars.
nameLengths(k, :) = [1, length(thisCellContents), length(thisCellContents)]; % The whole thing.
end
end
nameLengths % Report to command window.
[maxLength, row] = max(nameLengths(:,3)) % Find the longest one - biggest separation.
longestRow = a{row} % Get the entire string, including the start.
% Extract the substring.
out = longestRow(starLocations(1) + 1 : starLocations(2) - 1)
I'm sure there are less obvious, but more cryptic methods that are shorter if you want to wait for one from someone else.
  4 comentarios
Oblique
Oblique el 16 de Mayo de 2020
actually first code you write more clear than the other.I can understand what you write easily thanks for your help :)
Image Analyst
Image Analyst el 16 de Mayo de 2020
You're welcome. Because comments are SO important, I added a few to make it even easier to follow. I encourage you to use descriptive variable names and lots of comments in your code. If my answer helped you maybe you could click the Vote button. Thanks in advance.

Iniciar sesión para comentar.

Más respuestas (1)

Ameer Hamza
Ameer Hamza el 16 de Mayo de 2020
Editada: Ameer Hamza el 16 de Mayo de 2020
Try this
a = {'23*Ali*36' ; 'Veli*178' ; '99*Zeynep*'} ;
m = regexp(a, '[A-Za-z]*', 'match');
m = [m{:}];
[~, idx] = max(cellfun(@numel, m));
longest_name = m{idx};
Result
>> longest_name
longest_name =
'Zeynep'
  5 comentarios
Rik
Rik el 16 de Mayo de 2020
Editada: Rik el 16 de Mayo de 2020
I see no reason why this wouldn't work in Octave. I just tested it on 5.1.0 and it worked as expected. What errors are you getting?
Oblique
Oblique el 16 de Mayo de 2020
Editada: Oblique el 16 de Mayo de 2020
it did not work before but it worked now i dont know why happened like this. Thanks again

Iniciar sesión para comentar.

Categorías

Más información sobre Octave en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by