MATLAB Answers

Related to the findpeaks() function (Array indices must be positive integers or logical values.)

1 view (last 30 days)
EYUP KARTAL
EYUP KARTAL on 10 Jun 2020
Answered: Tanmay Das on 5 Aug 2021
I would like to embed below oscillation time formula in the logarithmic decrement formula where
Oscillation time:
I have a time vector time=[1x13094] and
for ii=1:length(time)
osc = diff(time); %oscillation period
end
i have created a oscillation period vector osc=[1x13093] by substracting consecutive time elements.
Logarithmic decrement:
Where y(t) are equal to peaks vector pks=[1x183] and pks vector coming from an accelaration vector acc=[1x13094] with findpeaks function:
[pks,locs]=findpeaks(acc);
Now, i want to use osc and pks vectors to calculate logarithmic decrement.
for ii=1:length(pks)
delta(ii)=log((pks(ii))/(pks(ii+osc(ii))));
end
When i try this code i get a error message 'Array indices must be positive integers or logical values.' How can i overcome this error? Thank you in advance.

Answers (1)

Tanmay Das
Tanmay Das on 5 Aug 2021
MATLAB does not support negative or zero indices in arrays. You can debug your code by placing Breakpoints at required location and then executing it step by step to know where negative or zero value is being passed as an index in the array. It is advised to follow a few things while passing a variable as an index to an array:
  1. One should be careful with syntaxes like pks(ii+osc(ii)) and check whether ii+osc(ii) becomes zero or negative at any step.
  2. One should check whether the array on which ii is iterating (i.e., time and pks in this case) has a non-zero length.
You can go through Matrices and Arrays and Array Indexing for further information.

Community Treasure Hunt

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

Start Hunting!