MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Miguelcm90 on 16 Sep 2013

Dear friends,

I have the following array, but I would like to do with a matrix but to start, It will be enough to see the results.

imput :

https://www.dropbox.com/s/or51e193j96vqs0/cte.txt

I can not put a shorter array because I need to check before in long ones.

First of all, I fix two values, a = min value, b = max value After that, I fix c as the number of consecutive values I want to consider.

So, taking into account above, I would have to loop through the array or whatever, to save a new array , which has the start from the c consecutive values between these two max and min restrictions.

To put an example, I have the following: a = 25 max value b = 15 min value c = 4 number of elements which satisfy conditions , that should be consecutive

321321 233321 546121 321516 321215 23123 23164 35465 31265 2155 2121 215 120 100 90 50 30 25 ->satisfies 20 ->satisfies 18 ->satisfies 15 ->satisfies 14 12 16 10 4 8 9

and the output should be the following:

25 20 18 15 14 12 16 10 4 8 9

and I would save a new array with this ouput. It should not take care if there is a value higher than the previous one, being the main condition, that have to be consecutives.

In the file, there is some NaN rows, so, It would be good if the solution skips this values. If anyone could give me the solution with the file I uploaded would be more than valuable. Because I need to test it in real data.

I hope to have been enough clear in my explanation.

Any help is welcome.

*No products are associated with this question.*

Answer by Andrei Bobrov on 17 Sep 2013

Edited by Andrei Bobrov on 17 Sep 2013

Accepted answer

x = dlmread('path_to_cte.txt'); z = x(~isnan(x)); t = z >= 25 & z <= 15; n = cumsum([false;diff(t) == 1]); out = z(n(end) == n);

or with use `strfind`

a = 25; b = 15; c = 4;

p = [false;z >= b & z <= a;false]; t = p(:)'; ii = [strfind(t,[0 1]);strfind(t,[1 0])-1]; out = z(ii(1,diff(ii)+1 >= c):end);

Answer by Roger Stafford on 17 Sep 2013

Call your column vector A.

f = find(diff([false;A<=a&A>=b;false])~=0); g = find(f(2:2:end)-f(1:2:end-1)>=c,1); B = A(f(2*g-1):end);

B is the result.

## 0 Comments