Calling matrix values to strings

1 次查看(过去 30 天)
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