MATLAB: How to change or compliment a particular bit in the binary string

MATLAB

Hi
I have number like a=50 I want to convert it to binary, then I want to randomly choose the position in a binary string and compliment it Like
a=50
b=dec2bin(a)
b=110010
e.g position=4
b becomes
b=110110
how I perform this. kindly help me

Best Answer

a=50 ;
b=dec2bin(a) ;
b1=110010 ;
b=str2double(regexp(num2str(b),'\d','match')) ;
pos = 4 ;
b(pos) = 1 ;
% join the numbers
iwant=str2double(sprintf('%1d',b)) ;