MATLAB: How to change a specific value in an array to 0

replace values

I am wanting to make a change to the value of an array at a specific position but not change the other values in the array. My example: n= [1:12] and UC is a 1×12 array calculated from n. When the value in n is 4, I am wanting to set UC at that corresponding position to zero, but keep the other calculated UC values. I cannot use if or any because those change the other UC values. Ultimately I am looking for a function that will set a specific position to a specific value in a defined array.

Best Answer

If I understand correctly what you want to do, this will work:
n = [1:12];
UC = randi(99, 1 , 12);
UC(n == 4) = 0;
This performs the replacement on ‘UC’ after it is calculated, so no other elements are changed.