MATLAB: Turning array values into values on an x axis

arraydoubleMATLAB

Suppose I had a double array and the values were 2,3,6 and 9.
How would I create a double array where all of the values are 1, except the 2nd, 3rd, 6th and 9th, which are 2?

Best Answer

x = ones(500,1);
indices = [2 3 6 9];
x(indices) = 2;
Or
x = ones(500,1);
x(indices) = x(indices)+1;