# Split large vector into smaller vectors based on upper and lower limits

5 views (last 30 days)
Joshua Pretorius on 12 Oct 2021
Hi all, i'm relatively new to MATLAB and coding in general, so i would love some help for a problem which is as follows.
I have a vector containing 6million data points representing wind speeds ranging from 0 to above 30 m/s. There is also another vector with corresponding datetimes.
For a job, when the wind speed reaches 20 m/s, i need to find the time taken for the wind speed to reach 30 m/s. So i would like to split the large vector into separate vectors where the first value is 20 and the final value is 30. These occurances happpen several times throughout the data, so i want to create an individual vector each time the wind speed exceeds 20 and reaches 30.
As an additional condition, the wind speed data is very messy, and there are sometimes single outliers which may be above or below the thresholds given above. Therefore, i would like the code to create the first point when there are 2 consecutive points above 20 m/s, and create the final point when there are 2 consecutive points above 30 m/s.
Any help would be greatly appreciated, thanks !
Mathieu NOE on 12 Oct 2021
hello
my 2 cents suggestion
why not first smooth a bit your data (using smoothdata)
then loop through the data and serach for conditions :
1/ start point is when 2 consecutive samples are in the band 20 +/- 1 (I need a certain tolerance here)
2/ stop point is when 2 consecutive samples are in the band 30 +/- 1 (I need a certain tolerance here)
3/ this portion of data is valid if there is max 1 (or tbd qty) outliers. The reason of data smoothing was to get rid of the single outliers problem somehow.
if you could share a portion of data , I could try my logic

Matt J on 12 Oct 2021
Edited: Matt J on 12 Oct 2021
Using this File Exchange submission,
vector = [8,29,18,21,23, 18,19,25,32,31 10,10, 21,26, 33,33];
D=discretize(vector,[-inf,20,30,inf],'IncludedEdge','right');
D=medfilt1(D,3); %outlier removal
x=(D<2);
D(x)=interp1(find(~x),D(~x),find(x),'previous');
Vectors=groupFcn(@(x) {x}, vector,groupTrue(D==2));
Vectors{:}
ans = 1×5
21 23 18 24 25
ans = 1×2
21 26
Joshua Pretorius on 9 Nov 2021
Thanks Matt, works perfectly ! Very much appreciated !