name as representing numbers
显示 更早的评论
Consider
1 个评论
Image Analyst
2020-1-21
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
回答(1 个)
Sindar
2020-1-21
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 个评论
Walter Roberson
2020-1-21
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
2020-1-21
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Package MATLAB Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!