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

5 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Stephan
Stephan il 25 Ago 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 Commenti
Stephan
Stephan il 26 Ago 2020
My pleasure. Matlab has a great documentation with many examples, which makes it easy to learn.
Stephen23
Stephen23 il 26 Ago 2020
Modificato: Stephen23 il 27 Ago 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:

Accedi per commentare.

Più risposte (1)

Stephen23
Stephen23 il 26 Ago 2020
Modificato: Stephen23 il 26 Ago 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'

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by