Assign a number to a letter in excel
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(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 个评论
Matt Tearle
2020-1-30
编辑: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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!