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

4 views (last 30 days)
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

Accepted Answer

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

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 26 Aug 2020
Edited: Stephen23 on 26 Aug 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'

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!