The most efficient way is to use bitget and bitset , as in your question, although you only need an 8-bit integer. Suppose you have an uint8 X array that describes your recorded data (example below, X = uint8([169;5]) ) for no particular reason. We can check the bits by creating a useful anonymous function:
>> dispbits = @(W) arrayfun(@(X) disp(bitget(X,1:8)),W) >> dispbits = @(W)arrayfun(@(X)disp(bitget(X,1:8)),W) >> dispbits(X) 1 0 0 1 0 1 0 1 1 0 1 0 0 0 0 0
and suppose you have a pattern , according to which you want to change the order of the bits stored in this integer vector:
>> pattern pattern = 1 8 2 7 3 6 4 5
You can use arrayfun and find to change the bit order according to pattern :
Y = arrayfun(@(X) uint8(sum(bitset(uint8(0),find(bitget(X,pattern))))), X) Y = 99 17
We get the required answer, effectively stored in a vector of 8-bit integers:
>> class(Y) ans = uint8 >> dispbits(Y) 1 1 0 0 0 1 1 0 1 0 0 0 1 0 0 0
source share