Replacing all 999 by zeros

2 views (last 30 days)
D
D on 25 Oct 2013
Commented: dpb on 26 Oct 2013
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

Accepted Answer

dpb
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
D
D on 26 Oct 2013
Thanks for giving me a clue about linear indexes and logical addressing. The problem with the way2 was 'subscript vs linear indexes'. The statement [a,b]=find(x==999.0) gives subscripts of the elements containing 999. Then x(a,b)=0 generates subscripts from all the combinations of elements of a and b and equate those elements equal to zero.
The statement a=find(x==999.0) rather gives the linear indexes which works well with statement x(a)=0
Any way the statement x(x==999)=0 is better than the above mesh.
dpb
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... :(

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!