Index exceeds the number of array elements (1). (2024)

1 view (last 30 days)

Show older comments

Marcos Granados Flores on 5 Mar 2021

  • Link

    Direct link to this question

    https://uk.mathworks.com/matlabcentral/answers/764291-index-exceeds-the-number-of-array-elements-1

  • Link

    Direct link to this question

    https://uk.mathworks.com/matlabcentral/answers/764291-index-exceeds-the-number-of-array-elements-1

Commented: Chris on 9 Mar 2021

Accepted Answer: Chris

Open in MATLAB Online

I want to create a movie with a for loop, but Index exceeds the number of array elements (1) is shown. Someone can tell me were is the error? The for loop without the condition works fine, but where i assign a condition, the error message appears. :( I just want that when y <= 0, the angle theta0 and v0 change to theta0 = atan(y(j-2)/(x(j)-x(j-2))) and v0 = 0.8v0.

clear, clc

g=9.81; theta0=45*pi/180; v0=5;

t(1)=0;x=0;y=0;

plot(x,y,'o','MarkerFaceColor','b','MarkerSize',8)

axis([0 8 0 0.8])

M(1)=getframe;

dt=1/128;

for j = 2:1000

t(j)=t(j-1)+dt;

x=v0*cos(theta0)*t(j);

y=v0*sin(theta0)*t(j)-0.5*g*t(j)^2;

plot(x,y,'o','MarkerFaceColor','b','MarkerSize',8);

axis([0 8 0 0.8]);

M(j)=getframe;

if y<=0

theta0 = atan(y(j-2)/(x(j)-x(j-2)));

v0 = 0.8*v0;

plot(x(j),0,'o','MarkerFaceColor','b','MarkerSize',8)

M(j) = getframe;

end

end

pause

movie(M,1)

2 Comments

Show NoneHide None

Chris on 6 Mar 2021

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/764291-index-exceeds-the-number-of-array-elements-1#comment_1372296

Open in MATLAB Online

I'm not entirely sure what value theta0 is meant to take when within that loop. y first turns negative when j=94, but don't forget that y is just a scalar the way you have it currently, so there's only one entry in position (1). In

theta0 = atan(y(j-2)/(x(j)-x(j-2)));

the first issue it encounters is y(j-2). At that point in the execution it becomes y(94-2) i.e. y(92) - the value of y in the 92nd column but y only has one entry/value, hence why you get that error.

Marcos Granados Flores on 6 Mar 2021

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/764291-index-exceeds-the-number-of-array-elements-1#comment_1372391

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/764291-index-exceeds-the-number-of-array-elements-1#comment_1372391

Open in MATLAB Online

Oh, the model must be able to predict exactly when the ball hits the ground. At this point, the direction changes (the new angle will equal the negative of the angle at impact), that's why i put a conditional if y<=0. This is an excercise from Chapra´s Applied Methods, Chapter 3, excercise 3.21. I found the exercise made with a while loop, but I want to make it with a for loop if is possible. Here is the code

g = 9.81;

theta0 = 50*pi/180;

v0 = 5;

t(1) = 0;

x = 0;

y = 0;

plot(x,y,'o','MarkerFaceColor','b','MarkerSize',8)

axis([0 3 0 0.8])

M(1) = getframe;

dt = 1/128;

j=2;

x0=0;

t_last = 0;

h_stop = 0.01;

h_max = Inf;

while h_max>h_stop % para los saltos

flag = 0;

h_max = -1;

while(flag == 0)

t(j) = t(j - 1) + dt;

x(j) = v0*cos(theta0)*(t(j)-t_last)+x0;

y(j) = v0*sin(theta0)*(t(j)-t_last) - 0.5*g*(t(j)-t_last)^2;

if y(j)>h_max

h_max = y(j);

end

if y(j)<= 0

% Calcular el nuevo ángulo

theta0 = atan(y(j-2)/(x(j)-x(j-2)))

flag = 1;

v0 = 0.8*v0;

x0 = x(j);

t_last = t(j-1);

plot(x(j),0,'o','MarkerFaceColor','b','MarkerSize',8)

axis([0 8 0 0.8])

M(j) = getframe;

else

plot(x(j),y(j),'o','MarkerFaceColor','b','MarkerSize',8)

axis([0 8 0 0.8])

M(j) = getframe;

end

j=j+1;

end

end

movie(M,1)

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Chris on 6 Mar 2021

  • Link

    Direct link to this answer

    https://uk.mathworks.com/matlabcentral/answers/764291-index-exceeds-the-number-of-array-elements-1#answer_640681

  • Link

    Direct link to this answer

    https://uk.mathworks.com/matlabcentral/answers/764291-index-exceeds-the-number-of-array-elements-1#answer_640681

Open in MATLAB Online

Right, I see what you're trying to do but the point stands that your modified code doesn't save the different values of y as time increases, it only stores the current value, so going into y(j-2) finds nothing unless j=3. Keeping them as follows would address that:

x(j) = v0*cos(theta0)*(t(j)-t_last)+x0;

y(j) = v0*sin(theta0)*(t(j)-t_last) - 0.5*g*(t(j)-t_last)^2;

I would also say the use of while in this example is well suited to the conditions set, but if you want to replace them with for, that's also possible. As you've done, if you don't require watetight conditioning (i.e. just to run a couple of examples) you can set a high number of for loops (i.e. 1000/2000/etc), but then it's also wise to have a condition that checks if it's not needed to keep on looping and exit (i.e. when the ball no longer bounches).

g = 9.81;

theta0 = 50*pi/180;

v0 = 5;

t(1) = 0;

x = 0;

y = 0;

plot(x,y,'o','MarkerFaceColor','b','MarkerSize',8)

axis([0 3 0 0.8])

M(1) = getframe;

dt = 1/128;

x0=0;

t_last = 0;

h_max = -1;

for j=2:2000

t(j) = t(j - 1) + dt;

x(j) = v0*cos(theta0)*(t(j)-t_last)+x0;

y(j) = v0*sin(theta0)*(t(j)-t_last) - 0.5*g*(t(j)-t_last)^2;

if y(j)>h_max

h_max = y(j);

end

if y(j)<= 0

theta0 = atan(y(j-2)/(x(j)-x(j-2)));

flag = 1;

v0 = 0.8*v0;

x0 = x(j);

t_last = t(j-1);

plot(x(j),0,'o','MarkerFaceColor','b','MarkerSize',8)

axis([0 8 0 0.8])

M(j) = getframe;

else

plot(x(j),y(j),'o','MarkerFaceColor','b','MarkerSize',8)

axis([0 8 0 0.8])

M(j) = getframe;

end

if x(j)==x(j-1)

break

end

end

movie(M,1)

This is one way you can do it all with one for loop. The condition:

if x(j)==x(j-1)

break

end

checks if the object is no longer moving in the x direction and exits the loop if that's the case. So for the default parameters that's 332 steps. If you double the gravitational acceleration, it only needs 170 steps and exits on its own (no point looping for the remaining 1830.)

2 Comments

Show NoneHide None

Marcos Granados Flores on 9 Mar 2021

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/764291-index-exceeds-the-number-of-array-elements-1#comment_1379287

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/764291-index-exceeds-the-number-of-array-elements-1#comment_1379287

Thank you so much!

Chris on 9 Mar 2021

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/764291-index-exceeds-the-number-of-array-elements-1#comment_1379317

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/764291-index-exceeds-the-number-of-array-elements-1#comment_1379317

👍

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABLanguage FundamentalsMatrices and ArraysCreating and Concatenating Matrices

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

  • for loop

Products

  • MATLAB

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Index exceeds the number of array elements (1). (7)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Index exceeds the number of array elements (1). (2024)

FAQs

Index exceeds the number of array elements (1).? ›

- The error message indicates that you're trying to access an element with an index larger than the array size, which leads to an "out of bounds" error. - To fix this issue, make sure you're using valid indices within the array's size, or consider increasing the array size if necessary to accommodate the desired index.

How do you find the index number of an element in an array? ›

JavaScript Array findIndex()

The findIndex() method executes a function for each array element. The findIndex() method returns the index (position) of the first element that passes a test. The findIndex() method returns -1 if no match is found.

How do you find the index of a maximum number in an array? ›

You could use findfirst(x -> x == maximum(X), X) or findfirst(==(maximum(X)), X) , but the easiest thing to do is argmax(X) .

What does "index exceeds array bounds" mean? ›

It means that the index number is more than the array elements.

What is the maximum number of elements the array can contain? ›

The theoretical maximum Java array size is 2,147,483,647 elements. To find the size of a Java array, query an array's length property.

What is index number in array? ›

Element index: It is the sequential number (index/key) assigned to the element where the first element of the array is assigned 0. It can also be defined as the number of elements prior to that particular element in the array. Size of a single element: Elements in the array need to be of the same data type or object.

How do you change the index number of an array? ›

Using the splice() Method

The splice() method is used to change an array by adding, removing, or replacing elements. This method modifies the original array. The first argument of the splice() method is the start index, which is the index at which to start changing the array.

What does index exceeds the number of array elements mean? ›

"Index exceeds the number of array elements" means you are indexing an array with some number n of elements, but asking for the m -th elements, where m>n . So if you had a vector x = [2 4 6] then x(1)=2 , but you can't do x(6) , for example, because x only has 3 elements.

How do you find the maximum count of a number in an array? ›

Description. M = max( A ) returns the maximum elements of an array. If A is a vector, then max(A) returns the maximum of A . If A is a matrix, then max(A) is a row vector containing the maximum value of each column of A .

What is the highest index that can be used with an array? ›

The indices allowed fall within a range, typically from 0 to the length of the array minus 1 (for a zero index origin) or 1 to the length of the array (for a 1 index origin). Thus, the highest index is a function of the index origin.

How do you fix array index out of bounds? ›

Here are some steps to resolve the exception:
  1. Review the code to find the line that triggered the exception. ...
  2. Check the values used for index variables and ensure they are within the valid range.
  3. Implement proper index validation to prevent the exception from occurring in the future.
Oct 27, 2023

How to prevent array index out of bounds exception in Java? ›

3. How to Avoid ArrayIndexOutOfBoundsException?
  1. 3.1. Remembering the Start Index. We must always remember that the array index starts at 0 in Java. ...
  2. 3.2. Correctly Using the Operators in Loops. Incorrectly initializing the loop variable to index 1 may result in ArrayIndexOutOfBoundsException. ...
  3. 3.3. Using Enhanced for Loop.
Jan 8, 2024

How to solve index exceeds matrix dimensions in MATLAB? ›

In MATLAB, an 'index exceeds matrix dimensions error' is encountered when we try accessing an element in the array/matrix which is not in the index or not present in the array/matrix. The simple way to resolve this error is by ensuring that we are trying to access the element in the matrix or array.

How do you find the maximum number from an array? ›

Iterative Approach to find the largest element of Array:
  1. Create a local variable max and initiate it to arr[0] to store the maximum among the list.
  2. Iterate over the array. Compare arr[i] with max. If arr[i] > max, update max = arr[i]. Increment i once.
  3. After the iteration is over, return max as the required answer.
Sep 13, 2023

What is the limit of array? ›

The maximum size of an array is determined by the amount of memory available in the system. When we create an array, the system reserves a contiguous block of memory to store the elements of that array. If we try to create an array of sizes greater than the available memory, the program may crash or give an error.

How do you find the index of an element in an array list? ›

The . indexOf() method returns the index of the first occurrence of the specified element in an ArrayList . If the element is not found, -1 is returned.

How do you find the index number of an element? ›

To find the index of an element in a list, you use the index() function. It returns 3 as expected. However, if you attempt to find an element that doesn't exist in the list using the index() function, you'll get an error. To fix this issue, you need to use the in operator.

How do you get the element at the index of an array? ›

The indexOf() method of Array instances returns the first index at which a given element can be found in the array, or -1 if it is not present.

What is the index of each element in the array? ›

The index of an array is a numerical value that represents the position of an element within the array. It typically starts at 0 for the first element, 1 for the second, and so on.

References

Top Articles
Latest Posts
Article information

Author: Rueben Jacobs

Last Updated:

Views: 6539

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Rueben Jacobs

Birthday: 1999-03-14

Address: 951 Caterina Walk, Schambergerside, CA 67667-0896

Phone: +6881806848632

Job: Internal Education Planner

Hobby: Candle making, Cabaret, Poi, Gambling, Rock climbing, Wood carving, Computer programming

Introduction: My name is Rueben Jacobs, I am a cooperative, beautiful, kind, comfortable, glamorous, open, magnificent person who loves writing and wants to share my knowledge and understanding with you.