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.

タグ

タグが未入力です。