http://www.mathworks.com/matlabcentral/newsreader/view_thread/241231
MATLAB Central Newsreader  Help with a Loop
Feed for thread: Help with a Loop
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Thu, 18 Dec 2008 01:09:03 +0000
Help with a Loop
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241231#617698
Ricardo Silva
Hi,<br>
I want to put the following statments in a loop, from 1 to 20:<br>
<br>
n1 = sqrt(diag(sec1'*sec1)); secn1= sec1./(ones(size(sec1,1),1)*n1');<br>
n2 = sqrt(diag(sec2'*sec2)); secn2= sec2./(ones(size(sec2,1),1)*n2');<br>
..........<br>
n20 = sqrt(diag(sec20'*sec20)); secn20= sec20./(ones(size(sec20,1),1)*n20');<br>
<br>
Any help?<br>
<br>
Thanks<br>
Rick

Thu, 18 Dec 2008 01:37:02 +0000
Re: Help with a Loop
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241231#617700
Roger Stafford
"Ricardo Silva" <ricardogs@terra.com.br> wrote in message <gic7rf$486$1@fred.mathworks.com>...<br>
> Hi,<br>
> I want to put the following statments in a loop, from 1 to 20:<br>
> <br>
> n1 = sqrt(diag(sec1'*sec1)); secn1= sec1./(ones(size(sec1,1),1)*n1');<br>
> n2 = sqrt(diag(sec2'*sec2)); secn2= sec2./(ones(size(sec2,1),1)*n2');<br>
> ..........<br>
> n20 = sqrt(diag(sec20'*sec20)); secn20= sec20./(ones(size(sec20,1),1)*n20');<br>
> <br>
> Any help?<br>
> <br>
> Thanks<br>
> Rick<br>
<br>
Instead of answering your question I will remind you that writing<br>
<br>
diag(sec1'*sec1)<br>
<br>
is a wasteful practice. It could be given as<br>
<br>
sum(sec1.^2,1).'<br>
<br>
which involves a lot less computation. All the computation for the offdiagonal elements was unnecessary.<br>
<br>
Also (ones(size(sec1,1),1)*n1' could be replaced by repmat(n1',size(sec1)) and would probably run faster. Multiplying by ones is very likely slower than making copies of an element.<br>
<br>
Roger Stafford

Thu, 18 Dec 2008 03:20:20 +0000
Re: Help with a Loop
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241231#617707
Roger Stafford
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gic9fu$6gb$1@fred.mathworks.com>...<br>
> .......<br>
> Instead of answering your question I will remind you that writing<br>
> <br>
> diag(sec1'*sec1)<br>
> <br>
> is a wasteful practice. It could be given as<br>
> <br>
> sum(sec1.^2,1).'<br>
> .......<br>
<br>
One correction. When I wrote sum(sec1.^2,1).' that is accurate only provided that all elements of sec1 are known to be realvalued. To preserve generality it would have been better to say that<br>
<br>
sum(conj(sec1).*sec1,1).'<br>
<br>
is the desired replacement for<br>
<br>
diag(sec1'*sec1)<br>
<br>
The expression sum(sec1.^2,1).' is the equivalent of diag(sec1.'*sec1).<br>
<br>
Roger Stafford

Thu, 18 Dec 2008 16:02:02 +0000
Re: Help with a Loop
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241231#617851
someone
"Ricardo Silva" <ricardogs@terra.com.br> wrote in message <gic7rf$486$1@fred.mathworks.com>...<br>
> Hi,<br>
> I want to put the following statments in a loop, from 1 to 20:<br>
> <br>
> n1 = sqrt(diag(sec1'*sec1)); secn1= sec1./(ones(size(sec1,1),1)*n1');<br>
> n2 = sqrt(diag(sec2'*sec2)); secn2= sec2./(ones(size(sec2,1),1)*n2');<br>
> ..........<br>
> n20 = sqrt(diag(sec20'*sec20)); secn20= sec20./(ones(size(sec20,1),1)*n20');<br>
> <br>
> Any help?<br>
> <br>
> Thanks<br>
> Rick<br>
<br>
I'm not sure, but I suspect the real question you are asking can be answered by Q4.6 in the MATLAB FAQ at:<br>
<br>
<a href="http://matlabwiki.mathworks.com/MATLAB_FAQ">http://matlabwiki.mathworks.com/MATLAB_FAQ</a>

Thu, 18 Dec 2008 17:47:23 +0000
Re: Help with a Loop
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241231#617882
Walter Roberson
Roger Stafford wrote:<br>
<br>
> Also (ones(size(sec1,1),1)*n1' could be replaced by repmat(n1',size(sec1))<br>
<br>
That would have to be repmat(n1', size(sec1,1),1)<br>
<br>
> and would probably run faster. Multiplying by ones is very likely slower than making copies<br>
> of an element.<br>
<br>
One would think so, but it turns out to be faster, at least when measured from the<br>
command line (rather than from a JIT'd .m file)<br>
<br>
>> sec1 = rand(501,603);<br>
>> n1 = sqrt(diag(sec1'*sec1));<br>
>> 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<br>
Elapsed time is 0.006225 seconds.<br>
Elapsed time is 0.005514 seconds.<br>
Elapsed time is 0.005053 seconds.<br>
<br>
Which is to say that repeated indexing is fastest, multiplying by ones is in the middle,<br>
and repmat() is the slowest.<br>
<br>
(Note: if you do the repeated indexing on the original n1 instead of the transposed n1,<br>
and then transpose the result, the algorithm is much slower, about 0.008 seconds.<br>
But the code is a lot easier to read, being n1(:,ones(1,size(sec1,1))).'<br>
The subsref() call is there just to avoid using a temporary variable that is then<br>
indexed.<br>
<br>
 <br>
.signature note: I am now avoiding replying to unclear or ambiguous postings.<br>
Please review questions before posting them. Be specific. Use examples of what you mean,<br>
of what you don't mean. Specify boundary conditions, and data classes and value<br>
relationships  what if we scrambled your data or used Inf, NaN, or complex(rand,rand)?