# Is there a way to crop an array depending on the values that I want through a function as the values change?

7 views (last 30 days)
Simeon on 19 Jul 2024
Answered: Mathieu NOE on 19 Jul 2024
I currently have a graph that has multiple flat maxima but I only want the index values of the last one. I am currently using islocalmax(data,'flatselection','all') to get all of the local maxima that exist and that then gives me a logical array which I can then either get the index values of all the 1's but I then want the last collection of 1's or the last collection of consecutive indexes. I was just wondering if there was a way to specifically select these values? I could manually find the start point of the last set and then set it to the end but as I want to use it on multiple data sets I want it to be automatic.
peaks = islocalmax(test.CAN_EMTrq,'FlatSelection','all');
peaks1 = find(peaks==1);
This is the code I am using right now.
The table either looks like: 000011111000100001111101111 and I want the last 4 1's or it looks like: 12,13,14,15,34,35,36,37,38,56,57,58,59 and I want the last 4 numbers.
Thanks

Mathieu NOE on 19 Jul 2024
hello
try this :
a = [0 0 0 0 1 1 1 1 1 0 0 0 1 0 0 0 0 1 1 1 1 1 0 1 1 1 1];
ind = logical(a);
[begin,ends] = find_start_end_group(ind);
duration = ends - begin;
% last group of duration >=3 consecutive samples
ind = duration>=3;
begin = begin(ind);
ends = ends(ind);
% start and end indexes of last buffer of 1's
begin = begin(end)
begin = 24
ends = ends(end)
ends = 27
function [begin,ends] = find_start_end_group(ind)
% This locates the beginning /ending points of data groups
% Important : ind must be a LOGICAL array
D = diff([0;ind(:);0]);
begin = find(D == 1);
ends = find(D == -1) - 1;
end

Image Analyst on 19 Jul 2024
The binary option is trivial if you have the Image Processing Toolbox
signal = logical([0,0,0,0,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,1,1,1,0,1,1,1,1]);
labeledSignal = bwlabel(signal)
labeledSignal = 1x27
0 0 0 0 1 1 1 1 1 0 0 0 2 0 0 0 0 3 3 3 3 3 0 4 4 4 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
(the display is being truncated and the 4's aren't showing up but they're there) but I don't know exactly what you mean when you say you want the last 4 1's. What does that mean exactly? Do you want to extract a vector of [1 1 1 1] or want their indexes or what????
for the floating point case you need to find out how flat is flat because it's a judgment call. You might want to call some kind of clustering method like dbscan or something. See
Once you've collected the data into clusters it will be easy to extract the last cluster. See this demo:
v = [12,13,14,15,34,35,36,37,38,56,57,58,59]
v = 1x13
12 13 14 15 34 35 36 37 38 56 57 58 59
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x = 1 : numel(v);
data = [v; x]'
data = 13x2
12 1 13 2 14 3 15 4 34 5 35 6 36 7 37 8 38 9 56 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
scatter(x, v);
result = dbscan(data,5,2)
result = 13x1
1 1 1 1 2 2 2 2 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
gscatter(x, v, result); % Visualize the groups it found
% Get last group
indexes = result == max(result)
indexes = 13x1 logical array
0 0 0 0 0 0 0 0 0 1 1 1 1
lastDataGroup = v(indexes)
lastDataGroup = 1x4
56 57 58 59
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

### Categories

Find more on Descriptive Statistics in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!