In this question, we only consider letters whose Morse Code representation consi
ID: 3796120 • Letter: I
Question
In this question, we only consider letters whose Morse Code representation consists of exactly three symbols (i.e. the letters D, G, K, O, R, S, U, and W). We will represent these three character Morse Code letters in Matlab by 1 times 3 arrays of class double where dots are represented by zeros and dashes are represented by ones. We will represent a sequence of n Morse Code letters in Matlab as a n times 3 array of class double, where each row represents a Morse Code letter and the rows have been concatenated vertically to form words. For example, the letter "D" is represented by the 1 times 3 array: (1, 0, 0] and the word "DOG" is represented by the 3 times 3 array: [1, 0, 0; 1, 1, 1; 1, 1, 0]. Write a function with the following header: function [word] = my_morse_to_word (morse) where: mores is a n times 3 array of class double that is the Morse Code representation of a word as described above. You can assume that n > 0. word is a 1 times n row vector of class char that represents the word whose Morse Code representation is described by morse. The letters in word should all be in upper case. Test cases: >> my_morse_to_ word ((1, 0, 0; 1. 1. 1; 1. 0]) ans = DOG >> my_morse_to_word ((0, 0, 0; 0, 1, 1; 1. 1, 1; 0, 1, 0; 1, 0, 0]) ans =Explanation / Answer
%matab code
function [word] = my_morse_to_word(morse)
word = '';
[m,n] = size(morse);
if (n != 3)
disp('Columns should be 3');
quit
end
for i=1:m
if ( morse(i,1) == 1 && morse(i,2) == 0 && morse(i,3) == 0 )
word = strcat(word,'D');
elseif ( morse(i,1) == 1 && morse(i,2) == 1 && morse(i,3) == 0 )
word = strcat(word,'G');
elseif ( morse(i,1) == 1 && morse(i,2) == 0 && morse(i,3) == 1 )
word = strcat(word,'K');
elseif ( morse(i,1) == 1 && morse(i,2) == 1 && morse(i,3) == 1 )
word = strcat(word,'O');
elseif ( morse(i,1) == 0 && morse(i,2) == 1 && morse(i,3) == 0 )
word = strcat(word,'R');
elseif ( morse(i,1) == 0 && morse(i,2) == 0 && morse(i,3) == 0 )
word = strcat(word,'S');
elseif ( morse(i,1) == 0 && morse(i,2) == 0 && morse(i,3) == 1 )
word = strcat(word,'U');
elseif ( morse(i,1) == 0 && morse(i,2) == 1 && morse(i,3) == 1 )
word = strcat(word,'W');
end
end
end
% test case
my_morse_to_word([1,0,0;1,1,1;1,1,0])
%output: DOG
my_morse_to_word([0,0,0;0,1,1;1,1,1;0,1,0;1,0,0])
%output: SWORD
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.