How to group arrays in a cell based from data?
Show older comments

I have this graph representing force vs. time. Then, I found its peaks to group them representing sort of a trend. I need to create two different cells, one for the forces with positive peaks and one for the negative ones, and two different cells of the location in the "x" axis (time) of the positive and negative peaks.
For example, for the negative peak forces, I was using histcounts.
pks_f_min = [ -68.0928; -63.6998; -110.9254; -103.2375; -132.8908; -125.2030; -137.2840; -113.1220; -148.2667; -144.9718; -151.5615; -159.2494; -151.5615 ]
[~,~,f_bins]=histcounts(pks_f_min, flip([0 -10 -20 -30 -40 -50 -60 -70 -80 -90 -100 -110 -120 -130 -140 -150 -160]));
u=unique(f_bins);
for k=1:numel(u)
groups_f_min{k}=pks_f_min(f_bins==u(k));
end
In the end, groups_f_min needs to look like this:
groups_f_min = { [ -68.0928; -63.6998 ] [-110.9254; -103.2375 ] [ -132.8908; -125.2030 ] [ -137.2840; -113.1220] [ -148.2667; -144.9718 ] [ -151.5615] [ -159.2494; -151.5615 ]}
Since I will be using this code for other graphs, is there a way to make it automatically?. For instance, other force patterns may need to be grouped in a cell consisting of two, three, or four arrays. For example, the array in the cell groups_f_min, which is alone (-151.5615), would be accompanied by another value
6 Comments
You define the bins used in histcounts according to the "y-axis", Force, and group the elements of pks_f_min according to those bins.
But then you say groups_f_min should be this:
groups_f_min = { [ -68.0928; -63.6998 ] [-110.9254; -103.2375 ] [ -132.8908; -125.2030 ] [ -137.2840; -113.1220] [ -148.2667; -144.9718 ] [ -151.5615] [ -159.2494; -151.5615 ]};
in which the elements of pks_f_min are grouped by their respective peak's location along the "x-axis", Time.
pks_f_min = [ -68.0928; -63.6998; -110.9254; -103.2375; -132.8908; -125.2030; -137.2840; -113.1220; -148.2667; -144.9718; -151.5615; -159.2494; -151.5615 ]
So if that's what you want to do, write down the times of the peaks (taken from the screen shot - 25 is a guess):
pks_t_min = [13 25 50 70 98 124 155 186 224 258 298 347 389];
And make the bins up to get the result you want:
[~,~,f_bins]=histcounts(pks_t_min,[0 40 80 140 200 260 300 400])
u=unique(f_bins);
for k=1:numel(u)
groups_f_min{k}=pks_f_min(f_bins==u(k));
end
groups_f_min{:}
Heirleking
on 23 Apr 2022
Edited: Heirleking
on 23 Apr 2022
Like you said, the bins for histcounts need to be specific to the problem, so to group the positive peaks, I would do the same thing as before but with different bins. It's not specified what the desired grouping is in this case, but here's a choice of bins that groups the positive peaks the same way the negative peaks were grouped:
pks_f_max = [ 70.2894; 63.6998; 105.4341; 95.5496; 128.4978; 123.0064; 137.2840; 132.8908; 142.7753; 143.8736; 154.8563; 160.3476; 143.8736 ] ;
pks_t_max = [ 6; 19; 36; 59; 82 ; 111; 135; 173; 204; 242; 277; 323; 369];
[~,~,f_bins]=histcounts(pks_t_max,[0 30 80 130 200 260 300 400]);
disp(f_bins.');
u=unique(f_bins);
for k=1:numel(u)
groups_f_max{k}=pks_f_max(f_bins==u(k));
end
groups_f_max{:}
It's not clear why you want to group the peaks in this particular way - in pairs adjacent in time but with the third to last peak alone. If you explain what you want to do with the peaks grouped like this, maybe a more general method or an alternative method that achieves your objective can be determined.
Heirleking
on 23 Apr 2022
Edited: Heirleking
on 23 Apr 2022
Voss
on 23 Apr 2022
In that case, I guess the bins for grouping (in both the positive and negative peaks cases) should be based on the times when the load is applied.
Heirleking
on 10 May 2022
Answers (0)
Categories
Find more on Logical 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!
