MATLAB: Hi every one , I need to construct a binary Array such that each time i can add another cell to it ? as shown in the code below .,, but i got an error

binary array

>> A1=[1 1 1 1 0 0 0 0];
>> D=[A1];
>> D(2)=[0 0 0 0 1 0 1 0];

Best Answer

Two of many ways...
>> D(2,:)=[0 0 0 0 1 0 1 0]; % have to make room for the vector in second dimension...
Alternatively,
>> D=[A1];
>> D=[D;[0 0 0 0 1 0 1 0]]
D =
1 1 1 1 0 0 0 0
0 0 0 0 1 0 1 0
>>
Either way, "preallocate, Preallocate!, PREALLOCATE!!" rather than dynamically resize arrays if at all possible.