string and mod function help

3 views (last 30 days)
Astha Sharma
Astha Sharma on 19 Oct 2015
Edited: Stephen23 on 19 Oct 2015
I'm trying to manipulate different parts of a string e.g. if the 4th number of a string is even then subtract the 5th number from the 6th number im not sure how to do this,so far i have (considering that my string is called c) :
c= input('enter provided code','s')
if (mod(c(4),3))
rvp = c(6)-c(5)
else
rvp= c(5)-c(6)

Accepted Answer

Stephen23
Stephen23 on 19 Oct 2015
Edited: Stephen23 on 19 Oct 2015
Try converting the input to a numeric vector, it will make your life much easier:
>> vec = input('enter provided code: ','s')-'0'
enter provided code: 123456
vec =
1 2 3 4 5 6
and to check if a value is even/odd, try using mod like with a value of 2, not 3:
if mod(vec(4),2)
rvp = vec(6)-vec(5)
else
rvp = vec(5)-vec(6)
end
produces this result (which is correct as vec(4) is even, so the difference vec(5)-vec(6) is calculated:
rvp = -1

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!