assigning numbers to string elements
Show older comments
Hi I have the following which creates 2 strings called left and right from a single string with the positions shifted by a single element.
pep='abcd';
sizepep=size(pep);
left=pep(1:(sizepep(2)-1));
right=pep(2:(sizepep(2)));
% so in this example left=abc and right = bcd
% if I were to assign numerical values to letters - eg.
a=1;
b=2;
c=3;
d=4;
How do I turn these two strings (left and right) into vectors so I can add each element together?
left=[1 2 3];
right=[2 3 4];
sum=[3 5 7]
Thank you in advance for advice.
1 Comment
Walter Roberson
on 25 Nov 2015
Please avoid naming a variable "sum" as that interferes with using the MATLAB routine named "sum"
Accepted Answer
More Answers (1)
Walter Roberson
on 25 Nov 2015
[~, left_code] = ismember(left, 'abcd');
[~, right_code] = ismember(right, 'abcd');
sum_code = left_code + right_code
Categories
Find more on NaNs in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!