How can I rewrite this to get peak locations?

3 次查看(过去 30 天)
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...

回答(2 个)

Star Strider
Star Strider 2023-2-10
编辑:Star Strider 2023-2-10
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
Lv = 10×1 cell array
{[ 1 1 1]} {[ 0 1 1]} {[ 1 1 0]} {[0 1 1 1]} {[ 0 1]} {[ 1 1 1]} {[ 1 1 1]} {[ 1 1 1]} {[ 0 1]} {[ 0 1 1]}
pksm = cellfun(@(x,y)x(y), pksc, Lv, 'Unif',0) % Peak Values Result
pksm = 10×1 cell array
{[ 0.8875 0.9108 0.2496]} {[ -0.3314 -0.1289]} {[ 0.3672 0.2208]} {[ 0.6036 0.8317 0.5501]} {[ 0.0153]} {[-0.1968 -0.5469 0.4821]} {[ 0.5430 0.8575 0.9401]} {[ 0.5961 0.3895 -0.1598]} {[ 0.9421]} {[ 0.5154 0.5672]}
locsm = cellfun(@(x,y)x(y), locsc, Lv, 'Unif',0) % Location Values Result
locsm = 10×1 cell array
{[3 5 8]} {[ 7 9]} {[ 4 6]} {[5 7 9]} {[ 4]} {[4 6 8]} {[2 4 8]} {[4 6 8]} {[ 7]} {[ 7 9]}
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 个评论
HC98
HC98 2023-2-14
Yes so sorry for not being very clear there. What I'd like to do is find peaks between 0.96 and 1.96 but when I do that it just returns an array of zeros for locsm :(
Star Strider
Star Strider 2023-2-14
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
Steven Lord 2023-2-10
Perhaps the islocalmax function with the dim input argument will meet your needs.

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by