Path: news.mathworks.com!not-for-mail
From: "Ali " <alihaider82@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Attempted to access ; index out of bounds
Date: Sun, 19 Apr 2009 23:29:01 +0000 (UTC)
Organization: University of Belgrade
Lines: 55
Message-ID: <gsgc3t$34d$1@fred.mathworks.com>
References: <gsfsh6$4m3$1@fred.mathworks.com> <gsftbd$q3i$1@fred.mathworks.com> <gsg8dq$akl$1@fred.mathworks.com> <gsgal1$vs$1@fred.mathworks.com>
Reply-To: "Ali " <alihaider82@gmail.com>
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 1240183741 3213 172.30.248.37 (19 Apr 2009 23:29:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 19 Apr 2009 23:29:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440530
Xref: news.mathworks.com comp.soft-sys.matlab:533950


"Jiro Doke" <jiro.doke@mathworks.com> wrote in message <gsgal1$vs$1@fred.mathworks.com>...
> "Ali " <alihaider82@gmail.com> wrote in message <gsg8dq$akl$1@fred.mathworks.com>...
> > "Jiro Doke" <jiro.doke@mathworks.com> wrote in message <gsftbd$q3i$1@fred.mathworks.com>...
> > > "Ali " <alihaider82@gmail.com> wrote in message <gsfsh6$4m3$1@fred.mathworks.com>...
> > > > i have written this code listed below:
> > > > 
> > > > far(1) = 0.001;
> > > > for i=2:10000000
> > > >     far(i+1) = (h(1400,0,far(i)) - h(684,0,0))/(0.999*43100 - h(1400,0,far(i)));
> > > >  
> > > >     if abs(far(i) - far(i+1)) <= 0.001
> > > >          
> > > >         break       
> > > >        far(i)
> > > >     else
> > > >         return
> > > >     end
> > > > end
> > > > 
> > > > it gives erroe: ??? Attempted to access far(2); index out of bounds because
> > > > numel(far)=1.
> > > > 
> > > > Error in ==> far_iter at 4
> > > >     far(i+1) = (h(1400,0,far(i)) - h(684,0,0))/(0.999*43100 -
> > > >     h(1400,0,far(i)));
> > > > 
> > > > sometimes it works without changing anything,sometimes it gives error. Plz help me.
> > > > Ali
> > > >    
> > > 
> > > Ali,
> > > 
> > > If you step through the code line by line, you'll notice that an error happens the first time through the loop. "i" starts from 2, so the first line reads:
> > > 
> > > far(3) = (h(1400,0,far(2)) - h(684,0,0))/(0.999*43100 - h(1400,0,far(2)));
> > > 
> > > Well, there is no far(2) yet, because you only have defined far(1).
> > > 
> > > It works sometimes, because you are probably not clearing the memory between runs.
> > > 
> > > jiro
> > 
> > I have changed value of far(1) = 0.015. It works but the answer is first value and the last vale, means two values come as output,how to make it right and what is the optimum value of iteartion.
> > ALi 
> 
> I'm not really sure what you are trying to do in your code. You have an if-else statement that checks for some condition, and you have both a "break" and a "return", which means that what ever you do, you will exit out of the loop in the first iteration. "break" will break out of the loop, and "return" will break out of the main function. Take a look at the help for "break" and "return".
> 
> So that's the problem.
> 
> jiro

Thanks. I have removed the return command, now it runs perfectly. thanks for your help and time.

Regards
ALi