How to group arrays in a cell based from data?

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 ]
pks_f_min = 13×1
-68.0928 -63.6998 -110.9254 -103.2375 -132.8908 -125.2030 -137.2840 -113.1220 -148.2667 -144.9718
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])
f_bins = 1×13
1 1 2 2 3 3 4 4 5 5 6 7 7
u=unique(f_bins);
for k=1:numel(u)
groups_f_min{k}=pks_f_min(f_bins==u(k));
end
groups_f_min{:}
ans = 2×1
-68.0928 -63.6998
ans = 2×1
-110.9254 -103.2375
ans = 2×1
-132.8908 -125.2030
ans = 2×1
-137.2840 -113.1220
ans = 2×1
-148.2667 -144.9718
ans = -151.5615
ans = 2×1
-159.2494 -151.5615
How would this protocol change for the positive ones? Assuming the positive peaks are
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 ] ;
And the location (time) of each
pks_t_max = [ 6; 19; 36; 59; 82 ; 111; 135; 173; 204; 242; 277; 323; 369]
I wasn't really able to use the same code for it. It definitely feels like I cannot just use this code for everything. But the range of values in the histcounts needs to be inputted depending on the grouping that is desired.
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.');
1 1 2 2 3 3 4 4 5 5 6 7 7
u=unique(f_bins);
for k=1:numel(u)
groups_f_max{k}=pks_f_max(f_bins==u(k));
end
groups_f_max{:}
ans = 2×1
70.2894 63.6998
ans = 2×1
105.4341 95.5496
ans = 2×1
128.4978 123.0064
ans = 2×1
137.2840 132.8908
ans = 2×1
142.7753 143.8736
ans = 154.8563
ans = 2×1
160.3476 143.8736
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.
The graph represents a displacement protocol from an experimental test for a structural element. After the test, the displacement is transformed into loading. There are three graphs resulting from this test: displacement vs. time, force vs. time, and force vs. displacement. The displacement protocol is transformed into two protocols. A loading one (with positive values, hence positive peaks) and an unloading one (with negative values, hence negative peaks). It is done in groups of two positive and two negative peaks (as shown in the displacement vs time graph) except for the 11th peak (+ and -), with only one loading and unloading pattern. Both displacement vs. time and force vs. time graphs are grouped similarly, but they will obviously have different histcount values. I have already found their peaks and locations for both graphs in time.
The displacement response graph shows a better representation to understand better why the grouping is in that particular way. As shown below, the 11th displacement peak (both + and -) is out of phase and is not coupled with another similar value. Thus resulting in that particular grouping way.
Displacement vs. Time
Force vs Displacement Relation (this graph is unnecesary for the purpose of the question)
I expected there would be a way to automatize the code for different displacement protocols for different elements where there could be groups of 3, 4, etc., and different ranges of peaks. Perhaps using a different command instead of histcounts?
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.
Do you mind explaining what do you mean by based on times ? Displacement peaks and force peaks do not occur at the same time

Sign in to comment.

Answers (0)

Products

Release

R2021a

Tags

Asked:

on 22 Apr 2022

Commented:

on 10 May 2022

Community Treasure Hunt

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

Start Hunting!