How to reduce the processing time of this specific block of code?

I made an algorithm in which I have to do this thing almost 100 times. I want to reduce the processing time of this block of code, can someone help me shorten the processing time or offer me an efficient block of code instead? in my laptop, this block is takin around "Elapsed time - 29.213608 seconds." when it is executed once. if I multiply this elapsed time by 100, then the answer will be about 48.7 minutes, which is a long processing time. if someone can help me with this, I will be grateful;) in fact, I want to invert the bits of a, and a is of type char, and I could not invert the bits using no operation. then I recognized it somewhere that does not work in matlab is not applicable to char type, so I first converted the bits from char to double, then applied not an operation,and then converted the resulting matrix back to char from double. my main goal is to invert char bits. if anyone can do it differently so plz help me. This thing takes a lot of processing time :(

tic;
a(1:1000000,1)='0';

c=str2num(a);

c=~c;

d=num2str(c);

toc;
+4
2

:

b = repmat('0',size(a));
b(a=='0') = '1';

:

ind = a=='0';
a(ind) = '1';
a(~ind) = '0';
+2

-

num2str((a-'0')==0)

ascii- -

char((a==48)+48) %// Thanks to Luis!

-

char(97-a)
+2

All Articles