MATLAB: For loop with horzcat

for loop horzcatMATLAB

Why does this not success?? Would someone help me correct the code?
A=[1 5; 3 3; 8 6; 4 8; 9 14; 11 11; 13 12; 15 7; 17 15; 1 20]
B=[]
for i=1:5
for j=1:5
B(i,:)=horzcat(B(i,:),A(i+j-1,:));
end
end
I'd like to get this
B=[1 5 3 3 8 6 4 8 9 14;3 3 8 6 4 8 9 14 11 11; 8 6 4 8 9 14 11 11 13 12; 4 8 9 14 11 11 13 12 15 7;9 14 11 11 13 12 15 7 17 15;11 11 13 12 15 7 17 15 1 20]

Best Answer

Another way,
AA=reshape(A.',[],1).';
B=cell(numel(AA),1);
for i=1:2:11
B{i}=AA(i:i+9);
end
B=cell2mat(B)