Replacing all 999 by zeros
2 views (last 30 days)
Show older comments
I have an array X of size 5000x10 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
0 Comments
Accepted Answer
dpb
on 25 Oct 2013
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.
2 Comments
dpb
on 26 Oct 2013
Yes, that's what I meant by list -- suppose could have been more specific and it would be agoodthing (tm) if the doc's explained this part of indexing syntax more clearly or at least in a more locatable place. I've not found it mentioned how it really works specifically.
As an aside, has TMW done anything to improve the help layout since R2012b? It really was a major step backwards in organization and usability and was quite a lot of discussion at the time a year to 18 months ago but it seems to have just passed away as a point. I suppose like so much, people just get resigned to it being what it is realizing the track of the Titanic can't be changed by a few gentle nudges from the shore... :(
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!