Path: news.mathworks.com!not-for-mail
From: "James Wright" <jameswright1001@yahoo.co.uk>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Help speeding up/replacing loops
Date: Wed, 17 Jun 2009 12:41:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 73
Message-ID: <h1ao8t$r0f$1@fred.mathworks.com>
References: <h18ge7$gta$1@fred.mathworks.com>
Reply-To: "James Wright" <jameswright1001@yahoo.co.uk>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1245242461 27663 172.30.248.35 (17 Jun 2009 12:41:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 17 Jun 2009 12:41:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1878517
Xref: news.mathworks.com comp.soft-sys.matlab:548254


Thanks for all the help so far, really appreciate it! This is an update of what my code looks like now, which I think is quite a bit better than before, but there's still a few points I could do with some help on.

function [] = the021test(a1,an,aacc,x,N1,N2,g)
N = N1-N2;
ncut = N/10;
b = 0;
count = 0;
kc = ((an - a1)/aacc);
%allocating vectors with the correct size, for memory reasons
B = zeros(1,N);
K = zeros(1,g);
m = zeros(1,ncut);
KC = zeros(1,kc);
A = zeros(1,kc);
%creates the vector (1,2,...,ncut)
X = 1:1:ncut;
%creates a vector with g random values
C(1:g) = (rand(1)*pi);
%loops everything for each value of a (aka mu), this is time consuming but
%I cannot see another way around it
for a = a1:aacc:an
    b = b + 1;
    A(b) = a;
    count = count + 1;
    %calculates the values of the transient
    for j = 1:N2
        x = a*x*(1-x);
    end
    %calculates the values to actually be used and the expectation
    B(1) = a*x*(1-x);
    for j = 2:N
        B(j) = a*(B(j-1))*(1-(B(j-1)));
    end
    E = mean(B);
    %calculates the p's and q's
    for f = 1:g
        p = cumsum(B(1:N).*cos((1:N)*C(f)));
        q = cumsum(B(1:N).*sin((1:N)*C(f))); 
        %calculates the m(n)s for the range of c
        for n = 1:ncut
            for j = 1:(N-n)
                m(n) = m(n) + (p(n+j)-p(j))^2 + (q(n+j)-q(j))^2;
            end
            m(n) = (m(n))/(N-n);
        end
        %calculates the oscillatory terms
        Vosc(1:n) = (E^2)*((1-cos((1:ncut)*C(f)))/(1-cos(C(f))));
        %Works out the new meansquare displacement with the oscillating
        %term taken out
        D(1:ncut) = m(1:ncut) - Vosc(1:ncut);
        %creates a vector containing the values of k for each value of c
        K(f) = corr(X,D);
    end
    %calculates the value of k to be plotted and stores it
    k = median(K);
    KC(b) = k;
    %outputs how far through the program you've gotten so far
    percents = ((count*aacc)/(an-a1))*100
end
%plots k against mu
plot(A,KC)



I'm wondering how I would vectorize

B(1) = a*x*(1-x);
for j = 2:N
     B(j) = a*(B(j-1))*(1-(B(j-1)));
end

Sorry if it seems like I'm asking for help on something that seems the same, but the problem I'm having is because B(j-1) is used twice. Is there any way to sort this out?
Thanks