MATLAB Answers

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

조회 수: 1(최근 30일)
EYUP KARTAL 2020년 6월 10일
답변: Tanmay Das 2021년 8월 5일
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
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:
Now, i want to use osc and pks vectors to calculate logarithmic decrement.
for ii=1:length(pks)
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.


Tanmay Das
Tanmay Das 2021년 8월 5일
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!

Translated by