Nested for loop to parse specific array indices not working?
이전 댓글 표시
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?
채택된 답변
추가 답변 (2개)
Image Analyst
2014년 6월 18일
Do you mean like this:
A=zeros(1,500); % Initialize
a2 = reshape(A, [50, 10]) % Make into 2D array.
% Set columns 5-8, for all rows of the array, = 1
a2(:,5:8) = 1
% Reshape back into a row vector.
A = a2(:)'
Andrei Bobrov
2014년 6월 18일
ii = [5,8];
n=10;
v = zeros(1,500);
v(ii(1):n:end) = 1;
v(ii(2)+1:n:end) = -1;
A = cumsum(v);
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!