How to make a morse code decoder using ismember?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have succesfully made a function to encode the statement AZ4M6NN0S789P to morse code. The series of dots and dashes were printed as a column vector for the final output. However I am trying to make a morse code decoder, which if I enter the column vector of dots and dashes, it should return "AZ4M6NN0S789P". I am trying to do this using ismember, and believe the solution could be similar to the code I used for the encoder function. Any suggestions on how to do this?
%my code for the encoder is as follows.
function encodedtext = morseEncoder()
text = input('Enter Characters to Encode \n','s' );
text = upper(text);
text = strjoin(strsplit(text));
code = {'.-';'-...';'-.-.';'-..';'.';'..-.';'--.';'....';'..';'.---';'-.-';'.-..';'--';'-.';'---';'.--.';'--.-';'.-.';'...';'-';'..-';'...-';'.--';'-..-';'-.--';'--..';'.----';'..---';'...--';'....-';'.....';'-....';'--...';'---..';'----.';'-----';'_'};
characters = {'A';'B';'C';'D';'E';'F';'G';'H';'I';'J';'K';'L';'M';'N';'O';'P';'Q';'R';'S';'T';'U';'V';'W';'X';'Y';'Z';'1';'2';'3';'4';'5';'6';'7';'8';'9';'0';''};
k =0 ;
for x=1:length(text)
[~,index]= ismember(text(x), characters);
if index >0
k = k +1 ;
fprintf('%s \n',code{index})
morseL{k} = code{index};
end
end
fprintf(morseL{k})
end
0 Kommentare
Akzeptierte Antwort
Jan
am 6 Dez. 2017
The final output is not a column vector, but a cell string. If it is a column vector, it would matter how you implement the breaks between the symbols. The character '' cannot appear in the string text. But there must be a method to distinguish ".- -." from ".--." .
If the output is a cell string as in your code, the conversion is trivial and needs one ismember command only. An equivalent method would simplify your code also:
code = {'.-';'-...';'-.-.';'-..';'.';'..-.';'--.';'....';'..';'.---';'-.-';'.-..'; ...
'--';'-.';'---';'.--.';'--.-';'.-.';'...';'-';'..-';'...-';'.--';'-..-'; ...
'-.--';'--..';'.----';'..---';'...--';'....-;'.....';'-....';'--...';'---..'; ...
'----.';'-----';'_'};
characters = ['A':'Z', '1':'9', '0']; % As char vector, not cell string
[~, index] = ismember(text, characters);
moresL = code(index);
And backward:
[~, index] = ismember(morseL, code);
text2 = characters(index);
If you really have a kind of character vector as output, use strsplit at the separators (which are missing as far as I can see) to get a cell string.
2 Kommentare
Jan
am 9 Dez. 2017
@Ryan: You did not explain, what you want as output of the encoding: is a cell string sufficient? Then:
function Out = Morse(In, cmd)
code = {'.-';'-...';'-.-.';'-..';'.';'..-.';'--.';'....';'..';'.---'; ...
'-.-';'.-..';'--';'-.';'---';'.--.';'--.-';'.-.'; ...
'...';'-';'..-';'...-';'.--';'-..-';'-.--';'--..'; ...
'.----';'..---';'...--';'....-';'.....';'-....'; ...
'--...';'---..'; '----.';'-----';'_'};
chars = ['A':'Z', '1':'9', '0'];
if strcmpi(cmd, 'encode')
[~, index] = ismember(In, chars);
Out = code(index);
else
[~, index] = ismember(In, code);
Out = chars(index);
end
end
Or if you want a char vector instead insert this:
...
if strcmpi(cmd, 'encode')
[~, index] = ismember(In, chars);
OutC = code(index);
Out = [sprintf('%s ', OutC{1:end-1}), OutC{end}]);
else
InC = strsplit(In, ' ');
[~, index] = ismember(InC, code);
Out = chars(index);
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!