MATLAB: Can *these loops* be vectorized

for loopspeedvectorization

I tried to vectorize any of these loops, but I couldn't do it. Strangely, when I try it out 'step-by-step' in the Matlab console, it works, but when changing it in my program (which can be found here) it doesn't work properly anymore.
Can anyone help me vectorize it OR make it otherwise faster? Thank you!
Info:
1. r is either 2,3 or 4
2. valuesBitwiseLonger is a binary number 132bits long
for u = 1 : r
ExOr1 = valuesBitwiseLonger(u);
ExOr2 = str2num(valuesBitwiseLonger(u + r));
exOrNew(u) = bitxor(str2num(ExOr1), ExOr2);
end
for l = 2*r : 132 - r + 1
NewExOr = '';
for p = 1 : r
NewExOr = horzcat(NewExOr, num2str(exOrNew(p)));
end
for u = 1 : r
ExOr1 = valuesBitwiseLonger(l + u - 1);
ExOr2 = str2num(NewExOr(u));
exOrNew(u) = bitxor(str2num(ExOr1), ExOr2);
end
end

Best Answer

str2num() of a string representing bits is going to start rounding off the numbers after 16.
Remember that str2num('11') is going to result in 11 decimal, which is 1011 binary. You do not want to be doing xor on the decimal interpretation of what are binary numbers.
You have no hope at all of reconstructing a 132 bit number accurately unless the first 116 bits are all 0. The closest you can get easily is 53 bits, using bin2dec(). The longest you can create in MATLAB as a numeric value is 64 bits (using uint64) but you have to be careful how you construct those.
You should completely avoid changing your bits to numeric form to do the xor. xor of two bits is equivalent to ~= (not equal). So take your inputs, pad the shorter on the left with 0's to make the two the same length, then use ~= between the two. The result will be data type logical, so do whatever you need to convert that back to the representation you are using for binary. For example if you are using '0' and '1' for your binary then
result_of_xor = char('0' + (first_value ~= second_value));
and that will be in character form all '0' and '1'