why do i get "Array indices must be positive integers or logical values " and how do i solve it ?

조회 수: 13 (최근 30일)
theta = [0:23] * pi/12 + 3*pi/8;
x = ones(1, 24) + sin(theta);
X = fft(x) / 24;
X_period = X([-11:12]);
disp('X[k] for one period =');
disp(X_period);
Array indices must be positive integers or logical values.
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 12월 3일
As the error message states - "Array indices must be positive integers or logical values."
X_period = X([-11:12]);
Here, you have provided negative values and 0 as indices, which is not allowed. That's why you get the error.
To resolve the error, don't provide values that are not allowed.

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

답변 (1개)

cr
cr 2023년 12월 3일
편집: cr 2024년 3월 18일
your X_period should equal to
X(1:24)
If you want to view it shifted you can have
idx = -11:12;
and either use
plot(idx,X)
or if you rather just display the numbers in text,
[idx; X]'
  댓글 수: 2
Steven Lord
Steven Lord 2023년 12월 3일
0 is not a valid linear index in MATLAB. Linear indices must be positive integer values, not non-negative.
cr
cr 2024년 3월 18일
편집: cr 2024년 3월 18일
Oops. Thanks for pointing out the oversight. Corrected it.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by