How can I rewrite this to get peak locations?
Show older comments
I have this
amps = arrayfun(@(h2) max((findpeaks(udata(h2, :), x) < 0.99)...
.* findpeaks(udata(h2, :), x)), 1:size(udata, 1));
And I want to rewrite it to get peak locations. When I call x(amps) it gives a logic error...
Answers (2)
Star Strider
on 10 Feb 2023
Edited: Star Strider
on 10 Feb 2023
The locations are the second output from findpeaks, so I doubt that arrayfun will do what you want.
I would do something like this —
udata = randn(10); % Create 'udata' Array
for h2 = 1:size(udata,2)
[pks,locs] = findpeaks(udata(h2, :));
pksc{h2,:} = pks; % Cell Array Of Peak Values
locsc{h2,:} = locs; % Cell Array Of Location Values
end
Lv = cellfun(@(x)x<0.99, pksc, 'Unif',0) % Cell Array Of Logical Vectors
pksm = cellfun(@(x,y)x(y), pksc, Lv, 'Unif',0) % Peak Values Result
locsm = cellfun(@(x,y)x(y), locsc, Lv, 'Unif',0) % Location Values Result
This will store the ‘pks’ and ‘locs’ vectors in their respective cell arrays. Process them appropriately later.
The ‘Lv’ (logical vector cell array) can have more than one condition, so that you can use a range of values such as:
@(x)(x<0.99) & (x>0.5)
if that is what you want to do. The rest of the code is unchanged.
EDIT — Corrected typographical errors.
.
6 Comments
HC98
on 13 Feb 2023
Try this —
Lv = cellfun(@(x) (x>1) & (x<2), pksc, 'Unif',0) % Cell Array Of Logical Vectors
Testing it with a random matrix —
udata = rand(500,15)*3; % Create 'udata' Array
for h2 = 1:size(udata,2)
[pks,locs] = findpeaks(udata(h2, :));
pksc{h2,:} = pks; % Cell Array Of Peak Values
locsc{h2,:} = locs; % Cell Array Of Location Values
end
Lv = cellfun(@(x)(x>1) & (x<2), pksc, 'Unif',0) % Cell Array Of Logical Vectors
pksm = cellfun(@(x,y)x(y), pksc, Lv, 'Unif',0) % Peak Values Result
locsm = cellfun(@(x,y)x(y), locsc, Lv, 'Unif',0) % Location Values Result
x = linspace(0, (size(udata,1)-1)/100, size(udata,1)); % Create Indepdent Variable Vector
for k = 1:numel(locsm)
if any(locsm{k})
xv{k,:} = x(locsm{k});
else
xv{k,:} = NaN;
end
end
xv
There are peaks in every vector, however only some of them meet the criteria (the ‘1’ values in ‘Lv’). The corresponding peak values are returned in ‘pksm’ and their location indices in the peak array are returned in ‘locsm’.
These results will need to be processed as individual cells (not as matrices), since it is unlikely that there will be the same number of columns in every cell.
Get the actual values of the independent variable vector (here ‘x’) at the peak locations as illustrated in the last loop (added here to my original code to demonstrate that). It includes a test to be certain that only ‘locsm’ cells with valid indices are addressed, returning NaN for the others. You can use that approach for other processing steps, as well.
.
HC98
on 13 Feb 2023
Star Strider
on 13 Feb 2023
My pleasure!
I do not understand what you want to do.
The ‘Lv’ assignment here will return true for peaks between 0.96 and 1.96. Peaks outside that range (including peaks equalling 0.75) will return false. In a logical vector, true values are denoted by 1 and false by 0. (The logical values are not inherently numerical values, although they become numerical values if you use them in calculations.)
HC98
on 14 Feb 2023
Star Strider
on 14 Feb 2023
That means that there are no peaks in that column that meet the criteria. You can verify that by looking at the ‘pksc’ cell for that particular column:
pksv = pksc{column_number}
You can see that also occurs in my demonstration in my previous Comment. There may not be any peaks in a particular column that meet the criteria, and in that instance, my code assigns the cell as NaN (all detected peaks are false).
Steven Lord
on 10 Feb 2023
0 votes
Perhaps the islocalmax function with the dim input argument will meet your needs.
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!