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:
Attempted to access ; index out of bounds

Subject: Attempted to access ; index out of bounds

From: Ali

Date: 19 Apr, 2009 19:03:02

Message: 1 of 6

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
   

Subject: Attempted to access ; index out of bounds

From: Jiro Doke

Date: 19 Apr, 2009 19:17:01

Message: 2 of 6

"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

Subject: Attempted to access ; index out of bounds

From: Ali

Date: 19 Apr, 2009 22:12:01

Message: 3 of 6

"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

Thanks Jiro. I have chenged now;
for i = 1:10000000

but now it didn't work. no result. What should I do now?

Ali

Subject: Attempted to access ; index out of bounds

From: Ali

Date: 19 Apr, 2009 22:26:02

Message: 4 of 6

"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

Subject: Attempted to access ; index out of bounds

From: Jiro Doke

Date: 19 Apr, 2009 23:04:01

Message: 5 of 6

"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

Subject: Attempted to access ; index out of bounds

From: Ali

Date: 19 Apr, 2009 23:29:01

Message: 6 of 6

"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

Tags for this Thread

No tags are associated with 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