MATLAB: Can’t get ny script running. What could be the problem

for loopmatrixrandom number generator

a=4:-1:4;
w=a*randn(350,1);
n = 400000;
proc_1= zeros(1, n);
for i = 1 : n
if rand() <= 0.5
proc_1(k) = x1(randi(length(x1)));
else
proc_1(k) = x2(randi(length(x2)));
end
resultant(k)=proc_1(k)+w(k);
end
i'm running this script but it keeps giving me this error=> " ??? Index exceeds matrix dimensions."

Best Answer

w=a*randn(350,1);
so w is length 350
n = 400000;
for i = 1 : n
so i will go to 400000
resultant(k)=proc_1(k)+w(k);
k is not defined in anything we see, but it could easily exceed 350. Especially if k is intended to be the same as i, in which case proc_1(k) would be valid up to k = 400000 but w(k) is only valid up to k = 350