# Assign a number to a letter in excel

12 ビュー (過去 30 日間)
Starg0rl 2020 年 1 月 29 日

I have a column on my excel spreadsheet (which i have loaded into matlab) where each cell contains either A B or C.
For example: X1 = [ A B A A A A A B C A B A A A B C A B A A A A A A B C ]'
I would like to extract from this column [A B] or [A B C]
I thought about assigning 1 to A, 2 to B, 3 to C so that the following column will then be as follows:
X2 = [1 2 1 1 1 1 1 2 3 1 2 1 1 1 2 3 1 2 1 1 1 1 1 1 2 3]'
from there, I would then use diff(X2) to get the consecutive values.
I am having trouble assigning the numbers to the letters. Is there a way of doing this? or is there a better way of going about this?

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

### 回答 (2 件)

Matt Tearle 2020 年 1 月 29 日
I don't understand what you're trying to get out of the original data. If you do diff(X2) you'll get
[1 -1 0 0 0 0 1 1 -2 1 -1 0 ...]
What would that mean (in terms of letters A, B, C)?
In the example you give (X1), what would the expected output be, and why?
Are you trying to remove repeats? That is, the result for X1 would be
[A B A B C A B A B C A B A B C]
If so, I'd suggest making sure X1 is string or categorical, then doing this:
Y = X1; % make copy (so X1 isn't altered)
Y(X1(1:end-1) == X1(2:end)) = [] % remove repeats (value is same as previous value)
##### 2 件のコメント表示非表示 1 件の古いコメント
Matt Tearle 2020 年 1 月 30 日
"i need to find their indices so that i can find the data that belongs to them on another column in excel"
OK, so what's the final goal? Because what you're describing there sounds like logical indexing:
X1 = [A A B A C A B B A C] % the groups
Y = [4 6 2 7 4 4 6 2 1 7] % the other data
>> Y(X1 == "A")
ans =
4 6 7 4 1
>> Y(X1 == "B")
ans =
2 6 2
>> Y(X1 == "C")
ans =
4 7
Can you give a simple example of what you're trying to get at the end?
If you really just want to turn letters into numeric values, you can use something like what Fangjun Jiang suggested (subtract 64 from the ASCII char value) or if you make X1 categorical, you can just do double(X1). But I don't think that's your ultimate goal, so there may be a better approach.

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

Fangjun Jiang 2020 年 1 月 29 日
X1={'A','B','C'}';
y=cell2mat(X1)-64

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

### Community Treasure Hunt

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

Start Hunting!
{"ft_lang_label":"__label__en","ft_lang_prob":0.8622118,"math_prob":0.87014127,"size":2241,"snap":"2021-43-2021-49","text_gpt3_token_len":761,"char_repetition_ratio":0.10773357,"word_repetition_ratio":0.12427185,"special_character_ratio":0.34895137,"punctuation_ratio":0.07285974,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9886502,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-11-30T16:05:24Z\",\"WARC-Record-ID\":\"<urn:uuid:a5ad335a-e02b-45bc-b620-c37eb728b822>\",\"Content-Length\":\"128560\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ad537132-84db-4767-8b2a-1bce4f941e89>\",\"WARC-Concurrent-To\":\"<urn:uuid:4f56d370-0b12-48ec-863b-a1a7fa9a6c1c>\",\"WARC-IP-Address\":\"23.36.36.88\",\"WARC-Target-URI\":\"https://jp.mathworks.com/matlabcentral/answers/502666-assign-a-number-to-a-letter-in-excel\",\"WARC-Payload-Digest\":\"sha1:E5JCSEYXNNTOMXDCB5YAXRM5TVCT3K24\",\"WARC-Block-Digest\":\"sha1:TM4NDUQY6ANATL52AOGTRDFNGNEVZKUM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964359037.96_warc_CC-MAIN-20211130141247-20211130171247-00101.warc.gz\"}"}