Index exceeds the number of array elements. Index must not exceed 1.

조회 수: 2 (최근 30일)
Stephen Moszynski
Stephen Moszynski 2022년 2월 1일
편집: KSSV 2022년 2월 1일
I have this error and im not sure how to correct it. I have read through other similar Q&A's but cant seem to figure out how to fix the error.
ERROR MESSAGE:
Index exceeds the number of array elements. Index must not exceed 1.
Error in untitled2 (line 6)
t4 = (0:N-1)/fs2(k); % Time vector
CODE:
close all;
N = 512; % Number of points, N
N2 = N/2; % Half N
fs2 = 1000; % Sample frequencies
for k = 1:2
t4 = (0:N-1)/fs2(k); % Time vector
f1 = (1:N)*fs2(k)/N; % Frequency vector
x4 = sin(2*pi*200*t4) + sin(2*pi*400*t4); % Signal
y4 = sin(2*pi*200*t4) + sin(2*pi*900*t4);
Xmag = abs(fft(x4)); % Magnitude spectrum
Ymag = abs(fft(y4));
plot(f1(1:N2),Xmag(1:N2),'r',f1(1:N2),Ymag(1:N2),'g');% Plot magnitude spectrum
end

채택된 답변

KSSV
KSSV 2022년 2월 1일
편집: KSSV 2022년 2월 1일
You are expecting fs2 to be of size 1*2. You have take k = 1,2 but your fs2 has only one value. Either take k = 1 alone or define fs2 as 1*2.
close all;
N = 512; % Number of points, N
N2 = N/2; % Half N
fs2 = [1000 2000]; %<---- changed here % Sample frequencies
for k = 1:2
t4 = (0:N-1)/fs2(k); % Time vector
f1 = (1:N)*fs2(k)/N; % Frequency vector
x4 = sin(2*pi*200*t4) + sin(2*pi*400*t4); % Signal
y4 = sin(2*pi*200*t4) + sin(2*pi*900*t4);
Xmag = abs(fft(x4)); % Magnitude spectrum
Ymag = abs(fft(y4));
plot(f1(1:N2),Xmag(1:N2),'r',f1(1:N2),Ymag(1:N2),'g');% Plot magnitude spectrum
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by