sorry for the bad description. I'm saying, the assignments between letters and numbers are given, what I want is to replace letter with numbers in the given way. The only method I was using is the if and else if statement with one-to-one assignment, but with longer string of letters, I don't really like to put 26 statement, is there any quicker way?
how to simplify multi- if statement
조회 수: 1 (최근 30일)
이전 댓글 표시
I'm trying to do the following work, given a string of 'ABCDE' etc,
for each letter, it will be assigned with a unique number.
How would I do it without putting 26 if and else if statement in a simple way?
I'm thinking of two matrices. But it may not be on the right track. Can anyone help with this case? Thanx in advance!
채택된 답변
Matt Fig
2012년 12월 6일
편집: Matt Fig
2012년 12월 6일
STR = char(65:90)
KEY = randi(10000,1,length(STR))
But what are you going to do with these that you would need an IF block for? Are you going to do something like encode a given string?
g_str = upper('Hello')
k_str = KEY(g_str-64)
댓글 수: 8
Matt Fig
2012년 12월 6일
But the same concept should apply. I subtracted 64 because we need to index starting from 1 and the ascii alphabet starts at 65.
Jos (10584)
2012년 12월 6일
Using randi, it is not guaranteed that each character in STR is mapped onto a unique value ...
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!