Roger Stafford wrote:
> Also (ones(size(sec1,1),1)*n1' could be replaced by repmat(n1',size(sec1))
That would have to be repmat(n1', size(sec1,1),1)
> and would probably run faster. Multiplying by ones is very likely slower than making copies
> of an element.
One would think so, but it turns out to be faster, at least when measured from the
command line (rather than from a JIT'd .m file)
>> sec1 = rand(501,603);
>> n1 = sqrt(diag(sec1'*sec1));
>> tic;repmat(n1.',size(sec1,1),1);toc,tic;(ones(size(sec1,1),1)*n1.');toc, tic;subsref(n1.',struct('type',{'()'},'subs',{{ones(1,size(sec1,1)) ':'}}));toc
Elapsed time is 0.006225 seconds.
Elapsed time is 0.005514 seconds.
Elapsed time is 0.005053 seconds.
Which is to say that repeated indexing is fastest, multiplying by ones is in the middle,
and repmat() is the slowest.
(Note: if you do the repeated indexing on the original n1 instead of the transposed n1,
and then transpose the result, the algorithm is much slower, about 0.008 seconds.
But the code is a lot easier to read, being n1(:,ones(1,size(sec1,1))).'
The subsref() call is there just to avoid using a temporary variable that is then
indexed.

.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships  what if we scrambled your data or used Inf, NaN, or complex(rand,rand)?
