Unable to perform assignment because the left and right sides have a different number of elements.

조회 수: 1 (최근 30일)
Hello,
I am working on a project that converts specific letters in a string to its numrical value.
For example: I want to convert letter 'h' in 'hello' into its numrical value and it should be '104ello' where 104=='h'
here is my code
inputMessage = "any string can be here";
message1 = char(inputMessage); % convert message from string to characters
loweredMessage = lower(message1);%convert the string to lower case
lengthMessage = length(message1); %Get the length of the characters
encodedMess=[];
for i = 1:length(message)
if message(i)=='a'|| message(i)=='e' || message(i)=='i' ||message(i)=='o' || message(i)=='u'
encodedMess(i) = num2str(double(message(i)));
else
encodedMess(i)=message(i)
end
end

채택된 답변

Walter Roberson
Walter Roberson 2019년 11월 10일
You can see that when you convert 'hello' to '104ello' that the output must be larger than the input. However, your code requires that the output be exactly the same as the input. You would try to replace 'a' by '97' but you would try to store that '97' into the single location encodedMess(i)
What you can do is initialize
encodedMess = "";
The output of the loop would then be a non-scalar string array
"97" "n" "y" " " "s" "t" "r" "105" "n" "g" " " "c" "97" "n" " " "b" "101" " " "h" "101" "r"
which you could then join together as a single string.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by