# Assigning letters to numbers

162 views (last 30 days)
Mike Keup on 8 Sep 2019
Edited: per isakson on 10 Sep 2019
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.

Adam Danz on 8 Sep 2019
What's the end goal here? What problems are you solving? Do capital letters have different values than lowercase letters?
You can convert letters to numbers using double().
double('Mike Keup')
ans =
77 105 107 101 32 75 101 117 112
sum(double('Mike Keup'))
ans =
827
mean(double('Mike Keup'))
ans =
91.889
David Hill on 8 Sep 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.

per isakson on 9 Sep 2019
Edited: per isakson on 10 Sep 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.
IMO functions makes it easy to divide a task into simpler subtasks. See Functions
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.)