Morse Decoder with Character Counter
조회 수: 7 (최근 30일)
이전 댓글 표시
I'm trying to program a morse decoder, in which you enter morse code (letters separated by '/') which then counts the frequencies of each character in the output.
Here is where I've gotten stuck:
clear all, close all,
clc
userMorse = '-.-./---/...-/../-../.----/----./..--.- /.--./.-/-./-.././--/../-.-./..--.- /...-/.-/-.-./-.-./../-././';
code = {'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-',...
'-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---',...
'.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..', '..--.-'};
letter = {'1','2','3','4','5','6','7','8','9','0',...
'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','-'};
codeTable = cell(2,37);
codeTable(1,:) = letter;
codeTable(2,:) = code;
splitUserMorse = strsplit(userMorse,'/');
for i = 1:length(splitUserMorse)
indexC = strfind(code,cell2mat(splitUserMorse(i)));
Index{1,i} = find(not(cellfun('isempty',indexC)));
end
댓글 수: 0
답변 (1개)
Alamanda Ponappa Poovaya
2021년 11월 9일
편집: Alamanda Ponappa Poovaya
2021년 11월 9일
Hi,
First of all, there are a few errors with your userMorse variable. Every time you have typed "..--.-", which indicates a '-' character, there is an additional space before the /. This will cause errors with string matching.
Another change needed is the last / at the end of the userMorse variable must be omitted, otherwise, the split function returns one blank element at the end.
You can see the code below, where I have made some changes. It should work now. In the end, I am saving your results in a table, as it would visually appear nicely
clear all, close all,
clc
userMorse = '-.-./---/...-/../-../.----/----./..--.-/.--./.-/-./-.././--/../-.-./..--.-/...-/.-/-.-./-.-./../-./.';
code = {'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-',...
'-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---',...
'.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..', '..--.-'};
letter = {'1','2','3','4','5','6','7','8','9','0',...
'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','-'};
splitUserMorse = strsplit(userMorse,'/');
indexC = zeros(1,length(splitUserMorse));
%Convert morse code to string
for i = 1:length(splitUserMorse)
idx = find(strcmp(code,splitUserMorse{i}));
indexC(i) = idx;
end
s = letter(indexC);
freq = zeros(1,37);
%find frequency of each character in string
for i = 1:length(letter)
freq(i) = nnz(find(strcmp(s,letter{i})));
end
%SAve results in table
sz = [37 2];
varTypes = ["string","double"];
varNames = ["Letter","Frequency",];
tlb = table('Size',sz,'VariableTypes',varTypes,'VariableNames',varNames);
tlb(:,1) = letter';
tlb(:,2) = num2cell(freq)';
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!