MATLAB: Filling a matrix with a loop

binaryfilfor looploopmatrixmatrixes

Hi community,
I want to create a large matrix 400×400, In this matrix I want it to have the form
A= [1 1 0 1 000000000000000000……..;0 1 1 0 1 00000000000000……; 0 0 1 1 0 1 00000000000000000] and so on till it is a 400×400 matrix.
I could not find a way yet to easiliy do this. As doing this manually is way too much work.
Does anyone know how to do this?

Best Answer

"Does anyone know how to do this?"
This is simple and efficient with toeplitz:
>> C = [1,zeros(1,399)];
>> R = [1,1,0,1,zeros(1,396)];
>> M = toeplitz(C,R);
>> size(M)
ans =
400 400
>> M(1:9,1:20) % first rows and columns
ans =
1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0
>> M(392:400,381:400) % last rows and columns
ans =
0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1