How do I fix "Index exceeds the number of array elements"?

n = 0:49;
x1 = 2*sin(3*(9*pi*n/50 + 3*pi/50));
% Determine period and plot 5 periods
N = length(n);
period = N;
while(period <= 2*N)
if(x1(1:period) == x1(period+1:2*period))
break;
end
period = period + 1;
end
Index exceeds the number of array elements. Index must not exceed 50.
fprintf("Period of x1 = %d\n", period);
figure;
stem(n, x1);
hold on;
for i = 1:4
stem(n + i*period, x1);
end
hold off;
title('x1');
xlabel('n');
ylabel('x1[n]');

Answers (1)

Well, period=50 so x1(period+1:2*period) is the same as x1(51:100). Since x1 has length 50, indexing it from 51:100 won't work.
n = 0:49;
x1 = 2*sin(3*(9*pi*n/50 + 3*pi/50));
% Determine period and plot 5 periods
N = length(n);
period = N
period = 50
I=(period+1:2*period)
I = 1×50
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
length_x1 = length(x1)
length_x1 = 50
x1(period+1:2*period)
Index exceeds the number of array elements. Index must not exceed 50.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2023a

Asked:

on 6 Jun 2023

Edited:

on 6 Jun 2023

Community Treasure Hunt

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

Start Hunting!