Assigning letters to numbers
142 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I want to come up with a script for the following...
assign letters to numbers but in reverse (A = 26, B=25, etc).
Then, use that to solve basic problems like
1. Sum of first name + Last name
2. Ratio of first name / Last name
3. Sum of first name raise to power of sum of last name
NAME: Keanu Reeves
** I would show what i have but I really have no idea what im doing. I tried to find examples but im still struggling bad.
3 Commenti
David Hill
il 8 Set 2019
function [morseText,morseSound] = text2morse(string,playSound)
%% Translate AlphaNumeric Text to Morse Text
string = lower(string);
%Defined such that the ascii code of the characters in the string map
%to the indecies of the dictionary.
morseDictionary = {{' ',' '},{'',''},{'',''},{'',''},...
{'',''},{'',''},{'',''},{'',''},{'',''},{'',''},...
{'',''},{'',''},{'',''},{'',''},{'',''},{'',''},...
{'0','-----'},{'1','.----'},{'2','..---'},{'3','...--'},...
{'4','....-'},{'5','.....'},{'6','-....'},{'7','--...'},...
{'8','---..'},{'9','----.'},...
{'',''},{'',''},{'',''},{'',''},{'',''},{'',''},...
{'',''},{'',''},{'',''},{'',''},{'',''},{'',''},...
{'',''},{'',''},{'',''},{'',''},{'',''},{'',''},...
{'',''},{'',''},{'',''},{'',''},{'',''},{'',''},...
{'',''},{'',''},{'',''},{'',''},{'',''},{'',''},...
{'',''},{'',''},{'',''},{'',''},{'',''},{'',''},...
{'',''},{'',''},{'',''},...
{'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','--..'}};
%Iterates through each letter in the string and converts it to morse
%code
morseText = arrayfun(@(x)[morseDictionary{x}{2}],(string - 31),'UniformOutput',false);
%The output of the previous operation is a cell array, we want it to be
%a string. This line accomplishes that.
morseText = cell2mat(morseText);
Here is an example that converts text to morse code that might help you.
Risposte (1)
per isakson
il 9 Set 2019
Modificato: per isakson
il 10 Set 2019
"assign letters to numbers but in reverse (A = 26, B=25, etc)"
Why not start by making a function that takes letters and returns numbers. letters2numbers would be an appropriate name.
An appropriate signature of the function would be
num = letters2numbers( word )
where the input, word, is a sequence of letters, e.g. first name, and num is a vector of numbers. letters2numbers can be implemented in many different ways. I came up with this one
function num = letters2numbers( word )
asc = double( upper(word) ); % http://www.asciitable.com/
num = 26 + double('A') - asc; % simple arithmetic
end
The Matlab function, double, returns the ascii numbers of the letters in the English alphabet.
>> asc = double( 'ABCZ' )
asc =
65 66 67 90
and
>> num = 26 + double('A') - asc
num =
26 25 24 1
converts the ascii-values to the numbers you asked for.
Try the function
>> letters2numbers('A')
ans =
26
>> letters2numbers('Reeves')
ans =
9 22 22 5 22 8
>>
"1. Sum of first name + Last name"
>> sum(letters2numbers('Reeves'))
ans =
88
Now it is possible to put together a function that does the homework
function out = homework( NAME )
cac = strsplit( NAME );
first = cac{1};
last = cac{end};
out(1) = sum(letters2numbers(first)) + sum(letters2numbers(last));
out(2) = sum(letters2numbers(first)) / sum(letters2numbers(last));
out(3) = sum(letters2numbers(first)) ^ sum(letters2numbers(last));
end
and run it
>> out = homework( 'Mike Keup' )
out =
125 1.2727 3.0227e+101
>> out = homework( 'Keanu Reeves' )
out =
171 0.94318 7.5661e+168
>>
If you find the above difficult to follow why not try Matlab Onramp
Two alternate implementations of letters2numbers
function num = letters2numbers( word )
len = length( word );
num = nan( 1, len );
word = upper( word );
for jj = 1:len
num(jj) = strfind( 'ZYXWVUTSRQPONMLKJIHGFEDCBA', word(jj) );
end
end
The statement
num(jj) = strfind( 'ZYXWVUTSRQPONMLKJIHGFEDCBA', word(jj) );
can be replaced by
num(jj) = find( 'ZYXWVUTSRQPONMLKJIHGFEDCBA' == word(jj) );
And the second alternate implementations
function num = letters2numbers( word )
[ ~, num ] = ismember( upper(word), 'ZYXWVUTSRQPONMLKJIHGFEDCBA' );
end
The function homework() doesn't see the differences. The inner is hidden from the calling function.
"I really have no idea what I'm doing" (I assume that you are serious.)
0 Commenti
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!