I've been getting an index exceed the number of array elemnts (2048)
How to I solve this?

답변 (2개)

Cris LaPierre
Cris LaPierre 2020년 7월 30일

0 개 추천

Use an index that is not greater than the dimension you are indexing. For example, if your array is a 1x2048, you will get this error is you try to index row 2 or column 2049.

댓글 수: 4

Jarren Berdal
Jarren Berdal 2020년 7월 30일
I started with a 2048 x 7 array, then sorted the data into 409 x 1 array. In the workspace its says 409 x 1 double
Cris LaPierre
Cris LaPierre 2020년 7월 30일
How are you indexing? That is what is causing the problem. What is the value of your index when you get the error?
Jarren Berdal
Jarren Berdal 2020년 7월 30일
편집: Walter Roberson 2020년 7월 30일
Pulling data from a 2048 x 7 array. Isolated some of the data into a 2048 x 1 array. Finding the mean of the data of groups of 5s. Came up with a 409 x 1 array.
error that appears " Index exceeds the number of array elements (2048) "
error in line 24
code:
17 - DATE = D(:,1); % first column, days numbered from 1/1/1900
%cloud of data for summ of the data in one grouping
19- Darray(1,1) = mean(DATE(1:N)); %initial value
20 - PRICE = D(:,5); % 5th column associated with closing price
21- Parray(1,1) = mean(PRICE(1:N));
22 - for i = 1:(R)
23 - p = (i*N);
24 - Darray((i+1),1) = mean(DATE((p+1):(p+N)));
25 - Parray((i+1),1) = mean(PRICE((p+1):(p+N)));
end
Cris LaPierre
Cris LaPierre 2020년 7월 30일
What is the value of
  • R
  • N (5?)
What is the size of DATE

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2020년 7월 30일

0 개 추천

We do not know what R is.
If we suppose R is the 409 and N is 5, then on the last iteration, when i = 409, then p = 409*5 = 2045, and p+1:p+N = 2046 : 2050, but the array is only 2048 long.
Meanwhile, when i = 1, then p=i*N = 5, and p+1:p+N is 5+1 : 5+5 = 6:10 . Notice that you have not accessed index 1:5 .
The solution:
23 - p = (i*N);
should be
23 - p = ((i-1)*N);

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2020년 7월 30일

댓글:

2020년 7월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by