How to remove first consonant/consonant cluster and add it to the end?

3 visualizaciones (últimos 30 días)
I am honeslty so lost at what to do? I thought it was simple since I understand loops and such but I keep gettign stuck. I did some research and it seems like ismember is the way to go here but I alos understand how regexpri might also be used. But as I am a beginner, I am not sure if it the best to utlize that function.
Anyways this is what I want as my result:
desk --> eskd; hello --> ello; cup --> upc; switch --> itchsw; etc...
I appreicate any sort of help !
a = ["desk", "hello", "cup", "switch", "smith", "flowers", "angry", "decorations"]
for i = 1:length(A)
if ismember xxxdd
xxx
end

Respuesta aceptada

Stephan
Stephan el 25 de Ag. de 2020
Here is a little function, that can do this
a = ["desk", "hello", "cup", "switch", "smith", "flowers", "angry", "decorations"]
iWant = FirstToLast(a)
function out = FirstToLast(a)
out = squeeze(char(a))';
out(:,size(out,2)+1) = out(:,1);
out(:,1) = [];
out = strrep(string(out)," ","")';
end
  3 comentarios
Stephan
Stephan el 26 de Ag. de 2020
My pleasure. Matlab has a great documentation with many examples, which makes it easy to learn.
Stephen23
Stephen23 el 26 de Ag. de 2020
Editada: Stephen23 el 27 de Ag. de 2020
@Karen Landeros: Note that this answer does not do what your question requests.
The given code just moves the first character to the end. It does NOT identify any consonants at all (leading, single, clustered, or otherwise), nor does it move them to the end, as the questions requests (and your examples show).
To identify consonants the code would have to use:

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 26 de Ag. de 2020
Editada: Stephen23 el 26 de Ag. de 2020
>> a = {'desk', 'hello', 'cup', 'switch', 'smith', 'flowers', 'angry', 'decorations'};
>> regexprep(a,'(^[^aeiou]+)(.+)','$2$1','once') % only leading consonant/s
ans =
'eskd' 'elloh' 'upc' 'itchsw' 'ithsm' 'owersfl' 'angry' 'ecorationsd'
>> regexprep(a,'([^aeiou]+)(.+)','$2$1','once') % first consonant/s (as question requests)
ans =
'eskd' 'elloh' 'upc' 'itchsw' 'ithsm' 'owersfl' 'ayngr' 'ecorationsd'
Note the difference for 'angry'

Categorías

Más información sobre Get Started with MATLAB 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