Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have codeword u and I need to make a program that allows :
for 1<=i<=k ,make all possible changes of l of the k bits in u.

Example 1:
if u = [1 1 0 1] ; k = 4 and l = 1


we obtain :
u1 = [0 1 0 1] ; u2 = [1 0 0 1] ; u3 = [1 1 1 1] and u4 = [1 1 0 0].


Example 2 :
if u = [1 0 0 ] ; k = 3  and l = 1


we obtain :
u1 = [0 0 0] ; u2 = [1 1 0] and u3 = [1 0 1].


I tried to program, but i obtain :

if k = 3 and l = 1
u1 = [1 00] ; u2 = [010] and u3 = [001]


What I have tried:

My code :

l = 1;
ind = nchoosek(1:k, l);
t = size(ind,1);
u1 = zeros(t,k);
u1(bsxfun(@plus, (ind-1)*t, (1:t).')) = 1; 
Posted
Updated 1-Jul-17 19:28pm
v2
Comments
PIEBALDconsult 1-Jul-17 19:49pm    
Please use "Improve question" to explain what you are trying to do more clearly.

1 solution

I do not know MATLAB and your requirement is not very clear but the following C code achieves the output of the examples
C++
int u = 0x0d;   // 1101
int k = 4;
int l, result;

for(l = 1 << (k - 1); l != 0; l >>= 1)
{
	result = u ^ l; // 0101 = 1101 XOR 1000, 1001 = 1101 XOR 100, 1111 = 1101 XOR 10, 1100 = 1101 XOR 1
}
// it just remains to express 'result' in the form [b1 b2 b3 b4] - where b1 .. b4 are the bit values

the above code also works for second example with 3 bits with of course appropriate changes in value of u and k.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900