string and mod function help

4 Ansichten (letzte 30 Tage)
Astha Sharma
Astha Sharma am 19 Okt. 2015
Bearbeitet: Stephen23 am 19 Okt. 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)

Akzeptierte Antwort

Stephen23
Stephen23 am 19 Okt. 2015
Bearbeitet: Stephen23 am 19 Okt. 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

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by