Dividing array to subarrays based on threshold
Show older comments
Hi. For an array, for example:
A=[0 1 2 3 4 3 2 1 0 1 1 2 3 7 8 4 3 2 1];
I'm trying to extract any subarray that goes beyond a certain threshold, fex x=2
So in the example above, I would be looking to extract [2 3 4 3 2] and [2 3 7 8 4 3 2]
I'm also looking for negative values (i.e. any subarray that goes below -2)
How can I do this in MATLAB? Thanks!
Answers (1)
Mathieu NOE
on 26 Feb 2021
this is my 2 cent suggestion
A=[0 1 2 3 4 3 2 1 0 1 1 2 3 7 8 4 3 2 1 0 1 2 4 8 9 6 3 2 1];
threshold = 2;
ind = find(A>=threshold);
ind2=find(diff(ind)>1);
ind_start = [ind(1) ind(ind2+1)];
ind_end = [ind(ind2) ind(end)];
for ck = 1:length(ind_end)
B{ck} = A(ind_start(ck):ind_end(ck));
end
Categories
Find more on Creating and Concatenating Matrices 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!