Dividing array to subarrays based on threshold

1 visualización (últimos 30 días)
PH39
PH39 el 26 de Feb. de 2021
Respondida: Mathieu NOE el 26 de Feb. de 2021
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!

Respuestas (1)

Mathieu NOE
Mathieu NOE el 26 de Feb. de 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

Community Treasure Hunt

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

Start Hunting!

Translated by