Clear Filters
Clear Filters

How can I group positive peaks in groups by their similar values ?

2 views (last 30 days)
So I have both positive and negative peaks for any given signal. I am interested in grouping them into positive and negative
pks_max =[
6.8285
6.0007
13.4501
13.4501
22.5547
23.3825
32.0733
31.6596
48.6273
49.4551
66.4228
85.4598
85.4598];
Here is my code for positive peaks, given that there is a same amount of positives as negatives, I could use the same code. This code worked when there were a same amount of values for each group, but it doesn't for my new case
% Get distance of every element to every other element.
distances_max = pdist2(pks_max,pks_max);
% Find out which pairs are within 1 of each other.
within2_max = distances_max > 0 & distances_max < 5;
% Erase upper triangle to get rid of redundancy
numElements_max = numel(pks_max);
j_max = logical(triu(ones(numElements_max, numElements_max), 0));
within2_max(j_max) = 0;
% Label each group with an ID number.
[labeledGroups_max, numGroups_max] = bwlabel(within2_max);
% Put each group into a cell array
for k = 1 : numGroups_max
[rows_max, columns_max] = find(labeledGroups_max == k);
indexes_max = unique([rows_max, columns_max]);
groups_max{k} = pks_max(indexes_max);
end
celldisp(groups_max); % Display the results in the command window.
  5 Comments
Heirleking
Heirleking on 22 Mar 2022
I did not show the negative peaks it is a signal. All I need to know is how to group these array of numbers
Image Analyst
Image Analyst on 22 Mar 2022
I see you accepted David's reply below so it appears that what you were really after was the histogram, you just didn't know the name of the function, and instead used words like peaks and groups. Well, glad you learned a new term and that David solved it for you. And thanks in advance for Accepting his answer to award him reputation points. Of course I suspect he has a prerelease version of the Mind Reading Toolbox.

Sign in to comment.

Answers (1)

David Hill
David Hill on 22 Mar 2022
[~,~,bins]=histcounts(pks_max,[0 10 20 30 40 50 60 70 80 90]);%or whatever bin edges you want
u=unique(bins);
for k=1:numel(u)
c{k}=pks_max(bins==u(k));%puts groups into different cell elements
end

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!