Fill array with NaNs


Time to time I need to filll an existing array with NaNs using logical indexing. A trick I discover is using arithmetics rather than filling. It is quite faster in some circumtances
A=rand(10000);
b=A>0.5;
tic; A(b) = NaN; toc
Elapsed time is 0.737291 seconds.
tic; A = A + 0./~b; toc;
Elapsed time is 0.027666 seconds.
If you know trick for other value filling feel free to post.
goc3
goc3 2024년 10월 28일
That is a clever trick.
Chen Lin
Chen Lin 2024년 10월 28일
Thanks for sharing this quick trick, Bruno. This marks your first post and we look forward to seeing more.

태그

아직 태그를 입력하지 않았습니다.