It looks like you are trying to change any values of w which are greater than wRes to be wRes instead, but you are instead placing an additional element into w at the (n+1)th position and then at the next iteration checking if that value is greater than wRes. I think what you are looking for is logical indexing.
The ith index of idx is true (1) if w(i) > wRes. Now you can use this to access the elements of w where idx is true and change only those values:
Consider this on a simpler example:
x = 1:10;
idx = x > 5;
x(idx) = -1;