name as representing numbers
10 views (last 30 days)
Show older comments
Consider
1 Comment
Image Analyst
on 21 Jan 2020
I don't understand the examples. Why is J = 1, and K (the last/fourth letter) = 2??? And what kind of name is NOML, and how do you get the sequence 1,2,26, 25 for NOML? In the meantime, here are some helpful snippets:
vec = linspace('A', 'Z', 26)
name = 'Manav'
numbers = upper(name) - 'A' + 1
vec =
Columns 1 through 21
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
Columns 22 through 26
86 87 88 89 90
name =
'Manav'
numbers =
13 1 14 1 22
Answers (1)
Sindar
on 21 Jan 2020
I think the only change you need from Image Analyst's is to subtract off the (numeric code for the) first letter of the name, rather than the first letter of the alphabet, then ensure everything is positive.
name = 'Manav'
% get the numeric code for each letter, then shift so the first letter corresponds to 0
numbers = upper(name) - upper(name(1));
% map negatives to positive (e.g. -1 -> 26) and count from 1
numbers = mod(numbers,26) + 1;
2 Comments
Walter Roberson
on 21 Jan 2020
numbers = mod(numbers,26) + 1;
Close but not quite. Consider 26. mod(26,26) is 0, and you would add 1 to get 1, whereas you would want to leave 26 as 26. For that matter, consider 1: mod(1,26) is 1, add the +1 to get 2, instead of leaving it alone as 1.
Sindar
on 21 Jan 2020
That's why I don't add 1 in the line above. So, the first letter maps to 0, then mod(0,26)=0, then 1. While the letter before it maps to -1, then mod(-1,26)=25, then 26.
See Also
Categories
Find more on Package MATLAB Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!