Index exceeds matrix dimensions.

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

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 2020년 4월 5일
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.

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

답변 (1개)

Image Analyst
Image Analyst 2020년 4월 5일

0 개 추천

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

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.

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2020년 4월 5일

댓글:

2020년 4월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by