Question
This is a MatLab Question, I have tried several versions of answers, but none could pass the test. Please help! Version 1: stringMetaOne = not(inputString=='
This is a MatLab Question, I have tried several versions of answers, but none could pass the test. Please help!
Version 1:
stringMetaOne = not(inputString==' '); stringMetaTwo = isletter(inputString); metaString = and(stringMetaOne,stringMetaTwo); [indicator,startPositionOfAllOnes,endPositionOfAllOnes,lengthofAllOnes]=FindConsecutiveOnes(metaString); for i = 1:length(startPositionOfAllOnes) word = inputString(startPositionOfAllOnes(i):endPositionOfAllOnes(i)); revWord = ReverseLetters(word); inputString(startPositionOfAllOnes(i):endPositionOfAllOnes(i))=revWord; end outputString=inputString; end
function sout = ReverseLetters(sin) sout = ''; if length(sin) == 1 sout(end + 1) = sin(1); else sout(end+1) = sin(1); for i = 1:length(sin)-2 if i % 2 == 0 sout(end + 1)= sin(length(sin) - i); else sout(end + 1) = sin(i); end end sout(end+1) = sin(end); end end
function [indicator,startPositionOfAllOnes,endPositionOfAllOnes,lengthofAllOnes] = FindConsecutiveOnes(inputArray) indicator = zeros(1, length(inputArray)); startPositionOfAllOnes=[]; endPositionOfAllOnes=[]; lengthofAllOnes =[]; j = 1; for i = 1 : length(inputArray) if i == 1 && inputArray(i) == 1 startPositionOfAllOnes(j) = i; elseif inputArray(i) == 1 && inputArray(i-1) == 0 startPositionOfAllOnes(j) = i; elseif i ~= 1 && inputArray(i) == 0 && inputArray(i-1) == 1 endPositionOfAllOnes(j) = i-1; lengthofAllOnes(j) = endPositionOfAllOnes(j)-startPositionOfAllOnes(j) + 1; j = j + 1; end end if inputArray(length(inputArray)) == 1
endPositionOfAllOnes(j) = length(inputArray);
lengthofAllOnes (j) = endPositionOfAllOnes(j) - startPositionOfAllOnes(j) + 1; end
for j = 1 : length(lengthofAllOnes)
indicator(startPositionOfAllOnes(j)) = lengthofAllOnes (j); end end
Version 2:
function [outputString] = ScrambleLetters(inputString)
onestring = not(inputString==' ');
twostring = isletter(inputString);
mstring = and(onestring,twostring);
[indicator,start_position,end_position,length_ones]=FindConsecutiveOnes(mstring);
%Iterate value
for i = 1:length(start_position)
word11 = inputString(start_position(i):end_position(i));
%Reverse the word11.
revword11 = ReverseLetters(word11);
inputString(start_position(i):end_position(i))=revword11;
%end loop.
end
%result
outputString=inputString;
end
%ReverseLetters function.m
function soutput = ReverseLetters(sin)
soutput = '';
if length(sin) == 1
soutput(end + 1) = sin(1);
else
soutput(end+1) = sin(1);
%Iterate value
for i = 1:length(sin)-2
if i % 2 == 0
soutput(end + 1)= sin(length(sin) - i);
else
soutput(end + 1) = sin(i);
end
end
soutput(end+1) = sin(end);
end
end
%consecutive numbers function.m
function [indicator,start_position,end_position,length_ones] = FindConsecutiveOnes(in_array)
indicator = zeros(1, length(in_array));
start_position=[];
end_position=[];
length_ones =[];
j = 1;
for i = 1 : length(in_array)
if i == 1 && in_array(i) == 1
start_position(j) = i;
elseif in_array(i) == 1 && in_array(i-1) == 0
start_position(j) = i;
elseif i ~= 1 && in_array(i) == 0 && in_array(i-1) == 1
end_position(j) = i-1;
length_ones(j) = end_position(j)-start_position(j) + 1;
j = j + 1;
end
end
if in_array(length(in_array)) == 1
end_position(j) = length(in_array);
length_ones (j) = end_position(j) - start_position(j) + 1;
end
for j = 1 : length(length_ones)
indicator(start_position(j)) = length_ones (j);
%Terminate the for loop.
end
end
Version 3:
function [outputString] = ScrambleLetters(inputString)
%Define two string variables
%Check for whitespace and letter.
stringMetaOne = not(inputString==' ');
stringMetaTwo = isletter(inputString);
%Meta String contains letter.
metaString = and(stringMetaOne,stringMetaTwo);
%Extract the results from FindConsecutiveOnes() function
[indicate,startPositionOfOnes,endPositionOfOnes,lengthofOnes]=FindConsecutiveOnes(metaString);
%Iterate through the string for the words.
for i = 1:length(startPositionOfOnes)
%Extract the word.
word = inputString(startPositionOfOnes(i):endPositionOfOnes(i));
%Reverse the word.
revWord = ReverseLetters(word) ;
%Reform the input string.
inputString(startPositionOfOnes(i):endPositionOfOnes(i))=revWord;
%Terminate the loop.
end
%Set the function result.
outputString=inputString;
%Terminate the function.
end
%Define the function to reverse the letters.
function sout = ReverseLetters(sin)
%Define a variable.
sout = '';
%Check for the string length.
if length(sin) == 1
sout(end + 1) = sin(1);
%Reverse the string.
else
%Store last value in the variable sout.
sout(end+1) = sin(1);
%Iterate through the length of the string.
for i = 1:length(sin)-2
%Check for even positions.
if i % 2 == 0
%Set the string.
sout(end + 1)= sin(length(sin) - i);
else
sout(end + 1) = sin(i);
%Terminate the inner if else.
end
%Terminate the loop.
end
sout(end+1) = sin(end);
%Terminate the outer if else.
end
%Terminate the function.
end
%Define the function to set the consecutive numbers.
function [indicate,startPositionOfOnes,endPositionOfOnes,lengthofOnes] = FindConsecutiveOnes(inputArray)
%Set the value of the function parameter.
indicate = zeros(1, length(inputArray));
startPositionOfOnes=[];
endPositionOfOnes=[];
lengthofOnes=[];
%Set the position.
j = 1;
%Iterate through the array.
for i = 1 : length(inputArray)
%Check the first charcter and position of the array.
if i == 1 && inputArray(i) == 1
%Set the start.
startPositionOfOnes(j) = i;
elseif inputArray(i) == 1 && inputArray(i-1) == 0
startPositionOfOnes(j) = i;
%Check for the last position and set the end.
elseif i ~= 1 && inputArray(i) == 0 && inputArray(i-1) == 1
endPositionOfOnes(j) = i-1;
%Set the length.
lengthofOnes(j) = endPositionOfOnes(j)-startPositionOfOnes(j) + 1;
%Set the position.
j = j + 1;
%Terminate the if elseif.
end
%Terminate the for loop.
end
%Check the array length and set the start and end position.
if inputArray(length(inputArray)) == 1
endPositionOfOnes(j) = length(inputArray);
lengthofOnes(j) = endPositionOfOnes(j) - startPositionOfOnes(j) + 1;
%Terminate the if else.
end
%Iterate through the length of the ones.
%Set the indicator as per the ons position.
for j = 1 : length(lengthofOnes)
indicate(startPositionOfOnes(j)) = lengthofOnes(j);
%Terminate the for loop.
end
%Terminate the function.
end
It dseon't mettar waht oedrr the Irettes in a wrod are Write a function ScrambleLetters to take a phrase consisting of words and non-letters, identify all the words (contain only letters) and then for all words reverse the subsequence of the letters leaving the first and the last letter of the word unchanged. The input phrase could be any length. The input sentence and output phrase should be a string scalar. For example: >>inputString "Ones and 1 is sometimes two"; >>[outputString] = ScrambleLetters (inputString) outputString- "Oens and 1 is semitemos two" Your Function Save C Reset MATLAB Documentation function [outputString] = ScrambleLetters ( inputString) 3 % Your new code goes here
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started