I want to map a letter to a a vector.
Example: a is mapped to [ 0 0 0 0 1]
b ia mapped to [0 0 0 1 0] and so on.
The containers.Map function does not help me with this regard. Is there any other way to do the mapping?
Thanks in advance!

 채택된 답변

Walter Roberson
Walter Roberson 2019년 7월 31일

0 개 추천

Map = dec2bin(('a' :'z') - 'a' + 1, 5);
Word = 'hello' ;
Map(Word(:) - 'a' + 1,:)

댓글 수: 3

Sanjana Sankar
Sanjana Sankar 2019년 7월 31일
Thanks. This answers my question as posted. But do you know how I should do it if I have other than just english alphabets? Or other characters? In gerneral, a scalar to vector mapping..
Create a character vector that contains all of the permitted characters. You can build it in pieces using [] if that helps.
Then
Map = dec2bin(1:numel(PermittedChars)) ;
[~, idx] = ismember(Word, PermittedChars);
Result = Map(idx, :);
Sanjana Sankar
Sanjana Sankar 2019년 8월 1일
Thanks a lot!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

태그

Community Treasure Hunt

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

Start Hunting!

Translated by