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

36 visualizzazioni (ultimi 30 giorni)
Hasan il 3 Dic 2023
Modificato: cr il 18 Mar 2024 alle 12: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 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Dyuman Joshi il 3 Dic 2023
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.

Accedi per commentare.

Risposte (1)

cr il 3 Dic 2023
Modificato: cr il 18 Mar 2024 alle 12:30
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 CommentiMostra NessunoNascondi Nessuno
Steven Lord il 3 Dic 2023
0 is not a valid linear index in MATLAB. Linear indices must be positive integer values, not non-negative.
cr il 18 Mar 2024 alle 12:30
Modificato: cr il 18 Mar 2024 alle 12:30
Oops. Thanks for pointing out the oversight. Corrected it.

Accedi per commentare.

Categorie

Scopri di più su Logical in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by