MATLAB: Replace an element of a vector by also redefining the vector

columnelementindexindexingnanreplacevector

Hi all,
I would like to replace some elements of a vector Vec that have indices idx with NaN, to create a new vector VecNaN. However, using the usual code below
Vec(idx) = NaN;
redefines the vector Vec with the NaN elements. Instead I want to find a new vector VecNaN such that it is the same as Vec, but with the NaN elements in the indices idx.
Thanks for your help in advance,
KMT.

Best Answer

You could do the following:
vecNaN = vec; vecNaN(idx) = NaN;