Index exceeds matrix dimensions.

2 views (last 30 days)
Boris Novosad
Boris Novosad on 5 Apr 2020
Commented: Boris Novosad on 5 Apr 2020
Hello people, I have this code and on the last row I am experiencing this error.
data - 886x1 double
time - 886x1 double
WL = 60; %nastaveni delky okna
kA = 7; %vahovaci konstanty
kF = 1;
mez = 2800; %nastaveni velikost meze
Fvz = 20;
nastdelka = length(data);
figure(2);
subplot(3,1,1);
plot(time,data); %zobrazeni vybraneho kanalu eeg signalu
xlabel('Čas [s]');
ylabel('Amplituda');
title('Pitch');
for n = 1:(nastdelka*Fvz-2*WL-1)
okno1=data(n:n+WL);
okno2=data(n+WL+1:n+1+2*WL);
Aw1(n) = sum(abs(okno1)); %vypocet amplitudy
Aw2(n) = sum(abs(okno2)); %jednotlivych oken
for m = 2:WL-1
Fw1(n) = sum(abs(okno1(m+1)-okno1(m-1))); %vypocet frekvence
Fw2(n) = sum(abs(okno2(m)-okno2(m-1))); %jednotlivych oken
end
G(n) = kA*abs(Aw1(n)-Aw2(n))+kF*abs(Fw1(n)-Fw2(n)); %mira rozdilu oken <-- ERROR OCCURANCE - Index exceeds matrix dimensions.
end
  2 Comments
Geoff Hayes
Geoff Hayes on 5 Apr 2020
Boris - when I run your code (with dummy values assigned to the data and time arrays) I see the same error on this line
okno2=data(n+WL+1:n+1+2*WL);
Given that we are iterating as
for n = 1:(nastdelka*Fvz-2*WL-1)
and nastdelka is the length of data which is 886, does this make sense? Or are your arrays larger than 886x1?
Boris Novosad
Boris Novosad on 5 Apr 2020
Hello geoff, yes, nastdelka = 886. I solved the problem by running the code without the Fvz, that means I don´t have to multiply nastdelka with Fvz. But I didn't understand why did the error appeared.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 5 Apr 2020
Put this as the first lines inside your for loop
if n+1+2*WL > length(data)
fprintf('Skipping n = %d because %d (which is n+1+2*WL) is longer than data, which has only %d elements in it.\n'...
n, n+1+2*WL, length(data));
continue;
end
Now look in the command window after your loop finishes. What do you see?
  1 Comment
Boris Novosad
Boris Novosad on 5 Apr 2020
Thanks Image Analyst, now I see why I can't run the loop properly.
Skipping n = 17689 because 17720 (which is n+1+2*WL) is longer than data, which has only 886 elements in it.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!