symmetrical character array with only letters

3 visualizaciones (últimos 30 días)
Elena
Elena el 20 de Feb. de 2022
Editada: DGM el 22 de Feb. de 2022
Im trying to make a function that gives a logical true if the character array is symmetrical and logical false if not.
So far I have:
a = 'abba'
isItSym = isequal(a(:),flip(a(:)))
res = isItSym
But for anything that is not A-Z or a-z it needs to come back as logical false and it does not (ex: 'p##p').
Im thinking some sort of if function with an sprintf to split the array up and then find any false values with char(0:64) and so on. Im a little new with matlab tho so im having trouble goin about this. Thanks!

Respuesta aceptada

Voss
Voss el 20 de Feb. de 2022
Editada: Voss el 20 de Feb. de 2022
Use isstrprop():
a = 'abba';
isItSym = isequal(a(:),flip(a(:))) && all(isstrprop(a(:),'alpha'))
isItSym = logical
1
a = 'p##p';
isItSym = isequal(a(:),flip(a(:))) && all(isstrprop(a(:),'alpha'))
isItSym = logical
0
  5 comentarios
Elena
Elena el 22 de Feb. de 2022
is was thinking
if myStr == '';
res = 0
else res = isItSym
but it doesnt seem to be working?
DGM
DGM el 22 de Feb. de 2022
Editada: DGM el 22 de Feb. de 2022
a = 'abba';
isItSym = ~isempty(a) && isequal(a(:),flip(a(:))) && all(isstrprop(a(:),'alpha'))
isItSym = logical
1
a = 'abbc';
isItSym = ~isempty(a) && isequal(a(:),flip(a(:))) && all(isstrprop(a(:),'alpha'))
isItSym = logical
0
a = 'a##a';
isItSym = ~isempty(a) && isequal(a(:),flip(a(:))) && all(isstrprop(a(:),'alpha'))
isItSym = logical
0
a = '';
isItSym = ~isempty(a) && isequal(a(:),flip(a(:))) && all(isstrprop(a(:),'alpha'))
isItSym = logical
0

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by