In: Computer Science
Write a passage and use Mathlab to display a matrix showing the code word and probability of occurrence of each character. thanks
ANSWER:
I have provided the properly commented
code in both text and image format so you can easily copy the code
as well as check for correct indentation.
I have provided the output image of the code so you can easily
cross-check for the correct output of the code.
Have a nice and healthy day!!
CODE TEXT
% defining paragraph
paragraph = "Write a passage and use Matlab to display a matrix " + ...
"showing the code word and probability of occurrence of each " + ...
"character. thanks";
% removing special character from paragraph using function regexpprep in
% matlab
filteredstring = regexprep(paragraph, '[%()\._+-,;:"$]+', '');
% spliting paragraph into words using split function
words = split(filteredstring);
% using function groupcounts to fetch count of each word in array
[counts,group] = groupcounts(words);
% finding probablity of occurenece, by dividing counts by total words
probablity = counts/length(words);
% displaying result
fprintf("Probablity of occurence of %s is %.2f.\n",[group'; probablity']);
CODE IMAGE
OUTPUT IMAGE