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

 채택된 답변

dpb
dpb 2013년 10월 25일

1 개 추천

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

D
D 2013년 10월 26일
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 2013년 10월 26일
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... :(

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

D
D
2013년 10월 25일

댓글:

dpb
2013년 10월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by