Does it exist a better way to reverse an element of X?
>> X = dec2bin(10)
X = 1010
I did this:
x(i) = num2str(1-str2num(x(i)))
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you want to flip a bit of a numeric value
numwithout converting it first to a character array of'0'and'1', then you can use functions like BITXOR, BITGET, and BITSET (as Andrey also mentions):However, if you do want to operate on the character array, you could also do this very strange thing:
This works because the characters
'a'andX(i)are first converted to their equivalent Unicode UTF-16 numeric values before the mathematical operation is performed. Since the numeric value for'a'is 97, then a'0'(numeric value 48) or'1'(numeric value 49) subtracted from'a'will result in the numeric value for the other. The resulting numeric value on the right hand side of the equation is then converted back to a character when it is placed back in the character arrayX.