How to ensure two vectors are the same size when using findpeaks?

조회 수: 4 (최근 30일)
iontrap
iontrap 2020년 10월 6일
답변: Utkarsh Belwal 2020년 10월 9일
I am using findpeaks to determine the local minima and maxima in a given input signal. I am then calculating the peak-to-peak difference between each of these minima/maxima pairs throughout the entire signal. Shown is my current code:
signal = myvector;
[pksmax,locsmax] = findpeaks(signal);
[pksmin,locsmin] = findpeaks(-signal);
realpksmin = -1*pksmin;
for j = 1:size(realpksmin)
pk2pkmaxmin(j) = abs(pksmax(j) - realpksmin(j));
end
Most of the time,
size(pksmax) == size(pksmin)
Sometimes these two vectors differ in size very slightly. If the above statement is false, then I have an error in my loop,
Index exceeds the number of array elements (154).
How can I ensure that
size(pksmax) == size(pksmin)
is always true upon using findpeaks(signal)?

채택된 답변

Utkarsh Belwal
Utkarsh Belwal 2020년 10월 9일
The number of local maxima and minima need not be same for every signal so you can’t get size of both the arrays same every time. A possible solution is to use array slicing and reduce the size of the large array to smaller one. Refer to the code below,
commonLength = min(length(pksmax) , length(pksmin));
pksmax_new = pksmax(1:commonLength);
pksmin_new = pksmin(1:commonLength);
Alternatively you can also insert zeros to the shorter array so that it’s length becomes equal to the longer one.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by