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

조회 수: 5 (최근 30일)
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

채택된 답변

Stephan
Stephan 2020년 8월 25일
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
Stephan
Stephan 2020년 8월 26일
My pleasure. Matlab has a great documentation with many examples, which makes it easy to learn.
Stephen23
Stephen23 2020년 8월 26일
편집: Stephen23 2020년 8월 27일
@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:

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2020년 8월 26일
편집: Stephen23 2020년 8월 26일
>> 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'

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by