How do I put peaks, prominences, widths and location in an array

Hi everyone!
Below is my code and I am try to incorporate the function findpeaks(y,'MinPeakProminence',0.1,) it almost works but the only problem is I get errors every time I try to put the minimum peak prominences into the arrays all peaks and findpeaks(y).
If anyone knows how to fix this I'd again be grateful!
fieldNames = fieldnames(MCR_full.MIB037.Reaches);
allPeaks = cell(10,1);
for k = 1 : 10
for k = 1 : 10
thisFieldName = fieldNames{k};
thisArray = MCR_full.MIB037.Reaches.(thisFieldName).kin;
x = thisArray(:, 1);
y = thisArray(:, 3);
[pks,locs,widths,proms] = findpeaks(y);
allPeaks{k} = findpeaks(y);
reachLimitArray= findpeaks(y,'MinPeakProminence',0.1)
if length(reachLimitArray )> 1
disp('There is at least two value above the limit.');
for i = 1 : length(reachLimitArray)
prominence= reachLimitArray(i);
disp(peaks(prominence));
end
else
disp('All values are below the limit.');
end
celldisp(allPeaks)
end
end
Error: Array indices must be positive integers or logical values.

4 Commenti

That’s what I’m trying to figure out. When I run each line separately, I can’t find the error but when I run the entire thing I get the error message below

 Array indices must be positive integers or logical values.

It doesn’t say what line though

My guess would be this line:
disp(peaks(prominence));
Can you not do that or did I just format it wrong?

Accedi per commentare.

 Risposta accettata

Since ‘prominence’ is the first output of that particular findpeaks call, it has the values of the peaks, not their locations (the second output, that would be integer subscript references), so unless the peak values are all integers greater than 0 and are within the number of elements of ‘peaks’ (extremely unlikely in my experience) they are going to be non-integers and will throw that error.

6 Commenti

So if I called it (index) and not (prominence)) would that work?
So long is it consists of integers that are all within the number of elements of the vector it’s indexing, you can call it anything you want!
So how do i create an array that has the prominences I want without having too many input arguments?
You essentially already did that with —
[pks,locs,widths,proms] = findpeaks(y);
You can then use ‘logical indexing’ (see the documentation on Matrix Indexing for details) to extract the corresponding values from the other vectors.
Example —
idx = proms >= 0.1; % Logical Index Of Prominences Meeting Requirements
pksDesired = pks(idx)
locsDesired = locs(idx)
widthsDesired = widths(idx)
promsDesired = proms(idx)
All the selected values will correspond to each other.
Experiment with this approach to get the desired results.
As always, my pleasure!
.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by