MATLAB: Replacing all 999 by zeros

assignassignment

I have an array X of size 5000×10 with some missing values. Missing values are represented by '999'. I tried following two ways to replace all '999' by zeros. The first way works correctly but second way replaces all the elements of some rows by zeros.
way1: X(find(X==999))=0; [ Works correctly ]
way2: [a,b]=find(x==999.0); x(a,b)=0; [ replaces all the elements of some rows by zeros ]
What is difference between two. Thanks for your answers

Best Answer

The latter isn't what you think it might be -- Matlab expands the subscript vectors to a list. As doc for find says, that form is really useful only for sparse matrices or w/ sub2ind or similar.
The neatest way to do what you're doing is using logical addressing directly...
x(x==999)=0;
You don't need find at all here.