Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

can't get all the other characters = 26

조회 수: 1 (최근 30일)
Benjamin Trivers
Benjamin Trivers 2020년 1월 26일
마감: MATLAB Answer Bot 2021년 8월 20일
Write a function m-file called stringcode that takes a string as input and produces an array containing a code for each letter in the string with A = 0, B = 1, ..., Z = 25 and all other characters getting the value 26.
function num = stringcode( word )
asc= double((word))
if asc >= 33 & asc <=47
asc = 26
else asc= double(upper(word))
end
asc= double(upper(word))
num = asc - double('A'); % simple arithmetic
end
  댓글 수: 2
James Tursa
James Tursa 2020년 1월 26일
asc is a vector, but your code treats it as a scalar. You either need to wrap your code in a loop to do each character one at a time, or you need to vectorize your code.
Benjamin Trivers
Benjamin Trivers 2020년 1월 26일
explain a little bit more?

답변 (1개)

Image Analyst
Image Analyst 2020년 1월 26일
You don't need all that complicated stuff. You almost had it at the end, so simply subtract 'A' and you're done. None of that foregoing stuff is needed. Watch:
>> num = 'ABCDEFGHIJKLMNOP' - 'A'
num =
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  댓글 수: 2
Benjamin Trivers
Benjamin Trivers 2020년 1월 26일
I still don't follow. Where does the num= go into?
Image Analyst
Image Analyst 2020년 1월 26일
In your function of course:
function num = stringcode( word )
num = word - 'A'
num(num<0 | num>25) = 26;
Just throw out all that useless stuff you had and have just those three lines. Basically the same as what you have except you don't need to cast to double

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by