string and mod function help

4 visualizzazioni (ultimi 30 giorni)
Astha Sharma
Astha Sharma il 19 Ott 2015
Modificato: Stephen23 il 19 Ott 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)

Risposta accettata

Stephen23
Stephen23 il 19 Ott 2015
Modificato: Stephen23 il 19 Ott 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

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by