Data selection using Nested Loop
1 view (last 30 days)
Show older comments
Hello Everyone, I'm trying to replace data in a cell array with NaN based on a range of values of max and min. In other words, if my data is below the minimum or the maximum range, then replace with NaN, if the data is within the range then leave the value as it is. This aims to filter the data. My data is stored in a cell array with time tables and the data is recorded every minute in average (sometimes more than 1 min). On the other hand, the range timetable is recorded every 24 hr.
nrows=size(TGC,1);
FDATA=cell(nrows,1);
A=cell(nrows,1);B=cell(nrows,1);CT1=cell(nrows,1);CT=cell(nrows,1); FDATA=cell(nrows,1);
for h=1:nrows
A=TGC{h,1}; %complete data set
k=size(C{h,1},1); %size of distributed data
kk=size(A,1); %size of complete data set
AT=datenum(A.Time); %time from complete data set
for hh=2:k
CT1=C{h,1}(hh-1,2); %time from distributed data -1
CT=C{h,1}(hh,2);%time from distributed data
for hhh=1:kk
ATG=AT(hhh,1);
if or(ATG >= CT1,ATG < CT) %Time comparison
CVar = C{h,1}(hh-1,3);%Maximum
DVar = D{h,1}(hh-1,3); %Minimum
AVar = A{hhh,3}; %UTM Easting
if CVar >= AVar && AVar >= DVar %is Avar within the max and min?
FDATA{h,1}(hhh,1)=AVar;
else
FDATA{h,1}(hhh,1)=NaN;
end
end
end
end
end
Note: FDATA is the cell array with the filter data. TGC is the raw data with a cell array of size 1x9, and each cell is a timetable with 2xn, data every ~1 min C is a cell array with the maximum values to filter TGC, data every 24 hr D is a cell array with the minimum values to filter TGC, data every 24 hr
The next figure shows the problem with the results from a single cell, as you can see the filter data (Distance) is not following the max and min range over time.
Thanks for any input you might have.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/167042/image.png)
0 Comments
Answers (1)
KSSV
on 6 Sep 2017
You need not to run a loop to achieve the task of replacing the values with NaN not lying in a given range. Check the below example code.
data = rand(100,1) ;
minval = 0.2 ; maxval = 0.8 ;
%%Make data NaN if the value is not in the above range
iwant = NaN(size(data)) ;
idx = data>=minval & data<=maxval ;
iwant(idx) = data(idx) ;
0 Comments
See Also
Categories
Find more on Graph and Network Algorithms 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!