name as representing numbers

10 ビュー (過去 30 日間)
mark@4882
mark@4882 2020 年 1 月 21 日
編集済み: mark@4882 2020 年 1 月 21 日
Consider
  1 件のコメント
Image Analyst
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
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
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
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePackage MATLAB Functions についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by