[matlab] Morse code decoder, please help me
49 ビュー (過去 30 日間)
古いコメントを表示
text1=string(input ('(Please Enter morse code: \n','s'));
morse={'.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..'};
letter={'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'};
for i=1:length(text1)
for j=1:length(letter)
if strcmpi(text1(i),morse(j))==1
disp(letter(j));
end
end
end
--------------------------------------------------------------------
This is my code.
When I enter '.-' on matlab commander it display 'A'. But if I enter '.- -...' to show 'A' 'B', Matlab can not read it.
Help.
2 件のコメント
Walter Roberson
2018 年 6 月 7 日
Hint: once you have broken the input up into pieces, you can match by using ismember()
Adam
2018 年 6 月 7 日
It would probably be more efficient to use
doc containers.Map
to put your morse letters as keys and ordinary letters as values (and vice versa if you want the reverse too)
採用された回答
Jan
2018 年 6 月 7 日
Or:
text1 = input('Please Enter morse code: \n', 's');
morse = {'.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..', ...
'--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-', ...
'-.--','--..'};
letter = char('A':'Z');
symbol = strsplit(text1, ' ');
for i = 1:length(symbol)
disp(letter(ismember(morse, symbol{i}))
end
Or without a loop:
[~, index] = ismember(symbol, morse);
disp(letter(index))
3 件のコメント
Jan
2018 年 6 月 11 日
The tilde ~ means, that this output argument is not used. It is equivalent to:
[dummy, index] = ismember(symbol, morse);
and then ignoring the value of the variable dummy. All you need for the decoding is the 2nd output of ismember.
その他の回答 (1 件)
Rishabh Rathore
2018 年 6 月 7 日
The problem with your solution is, you are comparing '.- -...' to '.-'.
First break the input string to individual morse letters and then run the loop.
Make the following changes in your code, it should work fine
text1=string(input ('(Please Enter morse code: \n','s'));
morse={'.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..'};
letter={'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'};
%-----Changes----
text1=textscan(text1,'%s','Delimiter',' '); %getting individual morse letters
text1=text1{1};
%----------------
for i=1:length(text1)
for j=1:length(letter)
if strcmpi(text1(i),morse(j))==1
disp(letter(j));
end
end
end
2 件のコメント
Maddie Long
2020 年 2 月 12 日
Hi, what if we want to send in mutiple letters? for example
test1 = .... ..
i get the error
"index exceeds the number of array elements"
thanks
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!