Error says "Subscript indices must either be real positive integers or logicals"

조회 수: 2 (최근 30일)
zero
zero 2018년 2월 14일
편집: Sam Chak 2023년 11월 1일
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9, 8, 7, 6, 5, 4, 3, 2, 1];
n = -9:9;
x1 = 5*x(n-2) - 3*x(n+4)
Array indices must be positive integers or logical values.
How to remove this error?
  댓글 수: 1
Sam Chak
Sam Chak 2023년 11월 1일
편집: Sam Chak 2023년 11월 1일
What is variable n? It seems that n indicates the relative position (left and right) from the mean of the data set x, which is an array. In array indexing in MATLAB, the sequential position are assigned as positive integers that begins from 1 (Python indexing starts at 0). This is the Rule in MATLAB.
So, you need to play with a little bit of arithmetics in your head, just as shown by bhai @Arun.

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

답변 (1개)

Arun
Arun 2023년 11월 1일
Hey,
here, the variable 'x' has index values ranging from 1 to 19. So to remove the error keep the index value with in that range of 1 to 19. For n-2 and n+4 both to be in range 1 to 19 the supported value for are in range 3 to 15. You can use the following code:
x = [1,2,3,4,5,6,7,8,9,9,9,8,7,6,5,4,3,2,1];
n = 3:15;
x1 = 5 * x(n-2) - 3*x(n+4)
x1 = 1×13
-16 -14 -12 -7 -2 6 14 22 30 33 36 34 32
another workaround can be to restructure the equation with respect to the size of 'x', one possible way is:
x = [1,2,3,4,5,6,7,8,9,9,9,8,7,6,5,4,3,2,1];
n = -9:9;
n = n + 10;
x1 = 5 * x(n)-2 - 3*x(n)+4
x1 = 1×19
4 6 8 10 12 14 16 18 20 20 20 18 16 14 12 10 8 6 4
Hope this gives you an idea to proceed.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by