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

5 vues (au cours des 30 derniers jours)
Karen Landeros
Karen Landeros le 25 Août 2020
Modifié(e) : Stephen23 le 27 Août 2020
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

Réponse acceptée

Stephan
Stephan le 25 Août 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 commentaires
Stephan
Stephan le 26 Août 2020
My pleasure. Matlab has a great documentation with many examples, which makes it easy to learn.
Stephen23
Stephen23 le 26 Août 2020
Modifié(e) : Stephen23 le 27 Août 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:

Connectez-vous pour commenter.

Plus de réponses (1)

Stephen23
Stephen23 le 26 Août 2020
Modifié(e) : Stephen23 le 26 Août 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'

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by