Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Help with a Loop

Subject: Help with a Loop

From: Ricardo Silva

Date: 18 Dec, 2008 01:09:03

Message: 1 of 5

Hi,
I want to put the following statments in a loop, from 1 to 20:

n1 = sqrt(diag(sec1'*sec1)); secn1= sec1./(ones(size(sec1,1),1)*n1');
n2 = sqrt(diag(sec2'*sec2)); secn2= sec2./(ones(size(sec2,1),1)*n2');
..........
n20 = sqrt(diag(sec20'*sec20)); secn20= sec20./(ones(size(sec20,1),1)*n20');

Any help?

Thanks
Rick

Subject: Help with a Loop

From: Roger Stafford

Date: 18 Dec, 2008 01:37:02

Message: 2 of 5

"Ricardo Silva" <ricardogs@terra.com.br> wrote in message <gic7rf$486$1@fred.mathworks.com>...
> Hi,
> I want to put the following statments in a loop, from 1 to 20:
>
> n1 = sqrt(diag(sec1'*sec1)); secn1= sec1./(ones(size(sec1,1),1)*n1');
> n2 = sqrt(diag(sec2'*sec2)); secn2= sec2./(ones(size(sec2,1),1)*n2');
> ..........
> n20 = sqrt(diag(sec20'*sec20)); secn20= sec20./(ones(size(sec20,1),1)*n20');
>
> Any help?
>
> Thanks
> Rick

  Instead of answering your question I will remind you that writing

 diag(sec1'*sec1)

is a wasteful practice. It could be given as

 sum(sec1.^2,1).'

which involves a lot less computation. All the computation for the off-diagonal elements was unnecessary.

  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.

Roger Stafford

Subject: Help with a Loop

From: Roger Stafford

Date: 18 Dec, 2008 03:20:20

Message: 3 of 5

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gic9fu$6gb$1@fred.mathworks.com>...
> .......
> Instead of answering your question I will remind you that writing
>
> diag(sec1'*sec1)
>
> is a wasteful practice. It could be given as
>
> sum(sec1.^2,1).'
> .......

  One correction. When I wrote sum(sec1.^2,1).' that is accurate only provided that all elements of sec1 are known to be real-valued. To preserve generality it would have been better to say that

 sum(conj(sec1).*sec1,1).'

is the desired replacement for

 diag(sec1'*sec1)

The expression sum(sec1.^2,1).' is the equivalent of diag(sec1.'*sec1).

Roger Stafford

Subject: Help with a Loop

From: someone

Date: 18 Dec, 2008 16:02:02

Message: 4 of 5

"Ricardo Silva" <ricardogs@terra.com.br> wrote in message <gic7rf$486$1@fred.mathworks.com>...
> Hi,
> I want to put the following statments in a loop, from 1 to 20:
>
> n1 = sqrt(diag(sec1'*sec1)); secn1= sec1./(ones(size(sec1,1),1)*n1');
> n2 = sqrt(diag(sec2'*sec2)); secn2= sec2./(ones(size(sec2,1),1)*n2');
> ..........
> n20 = sqrt(diag(sec20'*sec20)); secn20= sec20./(ones(size(sec20,1),1)*n20');
>
> Any help?
>
> Thanks
> Rick

I'm not sure, but I suspect the real question you are asking can be answered by Q4.6 in the MATLAB FAQ at:

http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Help with a Loop

From: Walter Roberson

Date: 18 Dec, 2008 17:47:23

Message: 5 of 5

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)?

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us