Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

index exceeds matrix dimension

2 views (last 30 days)
NIKITA
NIKITA on 1 Nov 2012
Closed: MATLAB Answer Bot on 20 Aug 2021
i am getting an error in the code to find peaks. Actually finding peaks is a function I am using for my main program to synchronize two files with respect to the time of the 2 files.
% pkind= index of the peak values % vlyind= index of valley % ind=index of sample when synch signal changes from hours to seconds
%% Peak Detection a=0; max = []; min = []; delta=0.15; % change this threshold if the peaks are not detected correctly d1=smooth(a); d2=1:length(d1); mn = 0; mx = 0; mnpos = 0; mxpos = 0;
lookformax = 1;
for i=1:length(d1) a1 = d1(i); if a1 > mx, mx = a1; mxpos = i; end if a1 < mn, mn = a1; mnpos = i; end
if lookformax
if a1 < mx-delta
max = [max ; mxpos mx];
mn = a1; mnpos = i;
lookformax = 0;
end
else
if a1 > mn+delta
min = [min ; mnpos mn];
mx = a1; mxpos = i;
lookformax = 1;
end
end
end
v1=max(:,2); v2=min(:,2); index=max(:,1); index2=min(:,1); t1=d2(index); t2=d2(index2); figure plot(d2,d1);hold on plot(t1,v1,'*r');hold on plot(t2,v2,'*r');hold off
The error is in v1,v2, index and index2. I am new to MATLAB and donot know the different functions which I could use or commands to handle the matrix dimension issue. I would be grateful if I could get some suggestions.

Answers (1)

John Petersen
John Petersen on 1 Nov 2012
First of all, it's probably not a good idea to use reserved words for variable names, such as max and min. These are standard function calls in matlab. You can type
>>help max
for example, to see if it is reserved.
Where does the data go? You have what seems to be the data in "a" but it's initialized to 0 and never changes. So you'll never find a max or min and all your vectors will remain empty. When you try to index into an empty array, you will get an error message.

This question is closed.

Community Treasure Hunt

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

Start Hunting!