MATLAB: Nested for loop to parse specific array indices not working

for loopfor loopslooploopsnestnestednested for loopsparse

So I am writing a code that parses over binary data but I have to put the data into an array for this to work. Basically, the array has a size of 500 bits (1/0s) and I treat these bits as if it were a 50 by 10 frame of data. So in other words every tenth bit represents a new row. I need to only assign data to columns 5-8 of each row and I am assigning these values a value of 1 while the rest are all zeros. So I am writing my function like this:
function A = createData()
A=zeros(1,500);
for i=5:10:110
for j=8:10:110
for k=i:j
A(k)=1;
end
end
end
end
But I am getting every bit from position 5 to position 108 filled with 1s rather than just the 4 bits per row of ten. Anybody know why this doesn't work or what I am missing?

Best Answer

This works:
A = zeros(1,500);
Ar = reshape(A, {}, 10);
Ar(:,5:8) = 1;
A2 = zeros(1,500)
for k1 = 1:10:500
A2(k1:k1+9) = Ar(fix(k1/10)+1,1:10);
end
A2
Note: My initial idea was to use Ar(:)' as the output but when I tried it, it didn’t produce the correct outuput.
A2 here produces:
A2 =
Columns 1 through 19
0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0
Columns 20 through 38
0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1
Columns 39 through 57
0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 ...
which is what I believe you want.