Asked by Mike Keup
on 8 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.

Answer by per isakson
on 9 Sep 2019

Edited by 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.

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.)

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Adam Danz (view profile)

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/479451-assigning-letters-to-numbers#comment_743614

## David Hill (view profile)

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/479451-assigning-letters-to-numbers#comment_743619

Sign in to comment.