string and mod function help
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Astha Sharma
el 19 de Oct. de 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)
0 comentarios
Respuesta aceptada
Stephen23
el 19 de Oct. de 2015
Editada: Stephen23
el 19 de Oct. de 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
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!