Thread Subject: Recursive Relation without using for loops

Subject: Recursive Relation without using for loops

From: selim selim

Date: 10 Oct, 2008 08:43:07

Message: 1 of 4

Hallo,

I have a recursive relation , I want to compute the elemenst of the series (W) without using for loops.
Here is the version with for loops.

-----------------------------------------------------------
% P is defined before

total=100;
time=100000;

W=zeros(total,time);

% First element

W(1,:)=1;


% Others are constructed using recursive relation
  
for m=2:total

    sum=zeros(1,time);
    
    for r=1:m-1
            sum(1,:)=sum(1,:)+W(r,:)*mean(P(m,:).*W(r,:))/mean(W(r,:).*W(r,:));
        
    end
     
    W(m,:)=W(m,:)-sum(1,:);

end

------------------------------------------------------------
This is indeed inefficient, takes hours to compute
Any suggestions will be appreciated

Thanks,

Selim




Subject: Recursive Relation without using for loops

From: Bruno Luong

Date: 10 Oct, 2008 12:16:23

Message: 2 of 4

"selim selim" <fthsel@example.com> wrote in message <gcn4ir$54o$1@fred.mathworks.com>...
> Hallo,
>
> I have a recursive relation , I want to compute the elemenst of the series (W) without using for loops.
> Here is the version with for loops.

Sorry I can't see any recursion in your algo/problem. On the other hand this array manipulation could be vectorized.

Bruno

Subject: Recursive Relation without using for loops

From: Matt Fig

Date: 10 Oct, 2008 18:56:02

Message: 3 of 4

This may very well be vectorizable, but you should also look at what your loops are doing to see if they can be improved. Since you you don't describe P, I ran a much shorter version of your code with a random P. I noticed that the resultant W is one column repeated over and over. There is no need to manipulate a matrix like this when you can do all you need with a vector. These simple changes get the same value for W, but not repeated in a matrix:


total = 100;
time = 100000;
P = round(rand(total,time)*20);

W = zeros(total,1);
W(1,:) = 1;

for m=2:total
    sum = 0;
    for r=1:m-1
        sum = sum + W(r,:)*mean(P(m,:).*W(r,:))/mean(W(r,:).^2);
    end
    W(m,:) = W(m,:)-sum;
end



This runs in 16 seconds on my machine. I didn't run the full matrix version, but if you say it was taking hours then you should see quite an improvement.

Subject: Recursive Relation without using for loops

From: John D'Errico

Date: 10 Oct, 2008 22:15:05

Message: 4 of 4

"Matt Fig" <spamanon@yahoo.com> wrote in message <gco8g2$rts$1@fred.mathworks.com>...
> This may very well be vectorizable, but you should also look at what your loops are doing to see if they can be improved. Since you you don't describe P, I ran a much shorter version of your code with a random P. I noticed that the resultant W is one column repeated over and over. There is no need to manipulate a matrix like this when you can do all you need with a vector. These simple changes get the same value for W, but not repeated in a matrix:
>
>
> total = 100;
> time = 100000;
> P = round(rand(total,time)*20);
>
> W = zeros(total,1);
> W(1,:) = 1;
>
> for m=2:total
> sum = 0;
> for r=1:m-1
> sum = sum + W(r,:)*mean(P(m,:).*W(r,:))/mean(W(r,:).^2);
> end
> W(m,:) = W(m,:)-sum;
> end

One thing that you should do is to NEVER
define a variable named sum, or any other
commonly used function name. This will
prevent you from being able to use those
functions. Look at what happens if you
do:

x = 1:.1:10;
sum = 5;

Can you now form the sum of the elements
in x? Try it.

sum(x)
??? Subscript indices must either be real positive integers or logicals.

John

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com