MATLAB: Quickly create a vector of ones and zeros

maskmathematicsMATLABvector

My question is if there is a way to create a vector of zeros
vec =
0 0 0 0 0 0 0 0
and specify a position lets say from the second until the fourth element to be ones.
vec =
0 1 1 1 0 0 0 0
Is there a quick way of doing this in Matlab avoiding loops?

Best Answer

vectorization is possible :
N=10;
vec=zeros(N,1);
positions=[2:4];
vec(positions)=1;
Related Question