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):» X=5*ones(1e6,1);
2. A better way (probably by Tim Love, tpl@eng.cam.ac.uk), which takes only 0.11s:
» X=repmat(5,1e6,1);
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
Y(idx)=0;