Is there a way to crop an array depending on the values that I want through a function as the values change?
4 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Mathieu NOE
2024-7-19
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)
ends = ends(end)
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
0 个评论
更多回答(1 个)
Image Analyst
2024-7-19
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)
(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]
x = 1 : numel(v);
data = [v; x]'
scatter(x, v);
result = dbscan(data,5,2)
gscatter(x, v, result); % Visualize the groups it found
% Get last group
indexes = result == max(result)
lastDataGroup = v(indexes)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Descriptive Statistics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!