string and mod function help

4 ビュー (過去 30 日間)
Astha Sharma
Astha Sharma 2015 年 10 月 19 日
編集済み: Stephen23 2015 年 10 月 19 日
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)

採用された回答

Stephen23
Stephen23 2015 年 10 月 19 日
編集済み: Stephen23 2015 年 10 月 19 日
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 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by