Find peaks in a matrix containing zeros

Dear All Community Members,
I have two matrices, 'A' and 'B'. Matrix 'A' represents the time history of velocities, while matrix 'B' represents the corresponding times. I want to extract all the peak velocity values from matrix 'A' that are above a threshold value of 3.
Matrix A is as follows:
A=[ 1 3 -1 1 5 -1 0;1 6 3 -2 0 0 0;2 3 9 -2 1 11 -1;4 1 -2 8 5 0 0;2 -2 6 -1 0 0 0;-1 13 -2 0 0 0 0];
Matrix B is as follows:
B=[1 1.5 2.5 3 3.5 4 0;1.5 3 5 6 0 0 0;0.5 2 2.5 2.75 3 4 5;2 4 4.25 4.5 6 0 0;1 2 3 4 0 0 0;0.5 3 4 0 0 0 0];
Specifically, I am looking to extract the value 5 from the first row, the value 6 from the second row, the values 9 and 11 from the third row, and then values 8,6,13 from the from the fourth and to the last row in matrix A.
I have tried the findpeaks function, but I am getting errors because of the ending zeros in matrix B.
Can anyone help me out?
Thank you in advance.

1 Comment

I am not sure if I get the logic used to extract the values.
In the 4th row, 4 is above 3, but they are not extracted.
How does the values from B come into play here?

Sign in to comment.

 Accepted Answer

A = [1,3,-1,1,5,-1,0;1,6,3,-2,0,0,0;2,3,9,-2,1,11,-1;4,1,-2,8,5,0,0;2,-2,6,-1,0,0,0;-1,13,-2,0,0,0,0]
A = 6×7
1 3 -1 1 5 -1 0 1 6 3 -2 0 0 0 2 3 9 -2 1 11 -1 4 1 -2 8 5 0 0 2 -2 6 -1 0 0 0 -1 13 -2 0 0 0 0
F = @(a)findpeaks(a,'Threshold',3);
C = cellfun(F,num2cell(A,2),'uni',0)
C = 6×1 cell array
{[ 5]} {[ 6]} {[9 11]} {[ 8]} {[ 6]} {[ 13]}

3 Comments

Not a comment on the answer, but on MATLAB.
I find it weird that islocalmax() considers 4 as a local maxima, but findpeaks() with a threshold of 3 does not consider it as a local maxima.
I am aware that the findpeaks() is defined as such, but then the labelling that it finds the local maxima is plainly wrong and 'maxima' should be removed from the documentation.
A = [1,3,-1,1,5,-1,0;1,6,3,-2,0,0,0;2,3,9,-2,1,11,-1;4,1,-2,8,5,0,0;2,-2,6,-1,0,0,0;-1,13,-2,0,0,0,0]
A = 6×7
1 3 -1 1 5 -1 0 1 6 3 -2 0 0 0 2 3 9 -2 1 11 -1 4 1 -2 8 5 0 0 2 -2 6 -1 0 0 0 -1 13 -2 0 0 0 0
islocalmax(A)
ans = 6×7 logical array
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 1 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
V = [1,1,2,4,2,-1]
V = 1×6
1 1 2 4 2 -1
X = findpeaks(V)
X = 4
Y = islocalmax(V)
Y = 1×6 logical array
0 0 0 1 0 0
By specifying the threshold explicitly then of course I can change what peaks are detected:
X = findpeaks(V, 'Threshold',3)
X = 1×0 empty double row vector
Clearly 4 is not >=3 compared to the 2's on either side, so it is (according to my requirement) not a maxima. In fact, there are no maxima in that vector according to the threshold that I specified.
"but then the labelling that it finds the local maxima is plainly wrong and 'maxima' should be removed from the documentation."
I do not follow your argument: the definition of what is a local maxima depends on the prominence, the window, etc... I do not see what is "plainly" wrong here.
Thank you for your quick response!

Sign in to comment.

More Answers (0)

Products

Release

R2020a

Tags

Community Treasure Hunt

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

Start Hunting!