MATLAB Answers

0

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

Asked by yaqoub alsarraf on 10 Nov 2019 at 20:58
Latest activity Commented on by Walter Roberson
on 11 Nov 2019 at 4:14
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

  0 Comments

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 10 Nov 2019 at 23:55
 Accepted Answer

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.

  2 Comments

How would I finally join together as a single string?

Sign in to comment.