Q: How to initialize an array with a constant?
Easy, huh? Well, there are at least a few ways:1. Most common one (on my machine it takes 0.24s):
2. A better way (probably by Tim Love, email@example.com), which takes only 0.11s:
3. And my favorite one(0.10s)
» X=zeros(1e6,1);X(:)=5;Note, that you can initialize selected elements with a constant as well, e.g. instead of
Y(idx)=zeros(length(idx),1);you can use much more elegant