필터 지우기
필터 지우기

Calling matrix values to strings

조회 수: 1 (최근 30일)
KA
KA 2015년 11월 15일
댓글: KA 2015년 11월 15일
Hi, I want to write a string of letters and then define a vector that will assign numerical values to each of the letters: e.g.
>> string='zxcv'
string =
zxcv
then define a vector which allows me to assign number to the string elements and add them:
vector=[1 4 6 -9 2 7 23 -6 -8 9 10 13 4 5 -8 -12 -2 1 0 11 -8 -9 3 8 9 2];
if string(1)=z
output=vector(2)+vector(4)
disp(output)
Matlab has a problem mainly with this line - if string(1)=1
Any clues?

채택된 답변

Stephen23
Stephen23 2015년 11월 15일
편집: Stephen23 2015년 11월 15일
In MATLAB the equality operator is ==, not =. This is clearly shown in the documentation (see link I gave). The single equals sign is only used to assign a value to a variable.
vector = [1,4,6,-9,2,7,23,-6,-8,9,10,13,4,5,-8,-12,-2,1,0,11,-8,-9,3,8,9,2];
string = 'zxcv';
if string(1)=='z' % note == not =
output = vector(2)+vector(4);
disp(output)
end
displays this:
-5
Note that for testing strings it is recommended to use strcmp or strncmp instead of array equals:
>> strncmp(string,'z',1)
rather than this:
>> string(1)=='z'

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by