Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Go from end to start of array
Date: Sat, 21 Feb 2009 23:13:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 50
Message-ID: <gnq1pu$mtt$1@fred.mathworks.com>
References: <gnmlkm$n25$1@fred.mathworks.com> <28689491.1235151366987.JavaMail.jakarta@nitrogen.mathforum.org> <gnnudm$7aj$1@fred.mathworks.com> <gnoa4p$j9q$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1235257982 23485 172.30.248.37 (21 Feb 2009 23:13:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 21 Feb 2009 23:13:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:519939

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gnoa4p$j9q$1@fred.mathworks.com>...
> Roger,
> 
> Can you indicates what I need to change in the parameters in the script below to force the two algorithms output the same result.
> 
> Thanks
> 
> Bruno
> 
> %%%%%%%%%%%%%
> 
> % Test case:
> n=10;
> m=4;
> a=(1:n);
> 
> % Basic algo
> i=1;
> while length(a)>1
>     disp(a)
>     i=mod(i+m-2,length(a))+1;
>     a(i)=[];
> end
> disp(a)
> ......
------------
Hi Bruno,

  Your parameters are all just right.  You simply neglected to start by deleting the first element of a.  To agree with Arthur's procedure you should have:

% Test case:
n=10;
m=4;
a=(1:n);

% Basic algo
i=1;
a(i) = []; % <-- Add this, RAS
while length(a)>1
    disp(a)
    i=mod(i+m-2,length(a))+1;
    a(i)=[];
end
disp(a)

That should end up with 2 in this case.

  Bear in mind that the two algorithms are doing quite different things as they proceed.  Only at the end should they agree as to the final value of 'a'.  My claim is that for very large values of n with m small, the while-loop method should be considerably faster because it has fewer steps to take.  The smaller the parameter m is, the greater the number of skipped steps.

Roger Stafford