convert vector of character to number

2 ビュー (過去 30 日間)
benghenia aek
benghenia aek 2022 年 5 月 22 日
編集済み: DGM 2022 年 5 月 22 日
hello
how to covert vectro cell ['A' 'A' 'A' 'B' 'B' 'A'] to numerical vector [1 1 1 2 2 1]

回答 (2 件)

MJFcoNaN
MJFcoNaN 2022 年 5 月 22 日
It depends on how you translate a word, for example:
% s in this form is not a cell, but a character array
s=['A' 'A' 'A' 'B' 'B' 'A']
s = 'AAABBA'
abs(s)-abs('A')+1
ans = 1×6
1 1 1 2 2 1

DGM
DGM 2022 年 5 月 22 日
編集済み: DGM 2022 年 5 月 22 日
Are the inputs always single characters? Are the inputs only A or B? Are the inputs always letters? Does case matter?
Here's a start for a method. It's compact and case-insensitive, but note that the outputs will probably be unexpected if the inputs aren't letters or single characters.
bunchofletters = {'A' 'A' 'A' 'B' 'B' 'A'};
bunchofnumbers = double(lower([bunchofletters{:}]))-96
bunchofnumbers = 1×6
1 1 1 2 2 1
Without knowing what the requirements are, I'm going to leave the task of making it fit for purpose up to someone who knows what the purpose is.

カテゴリ

Help Center および File ExchangeLaTeX についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by