Data selection using Nested Loop

1 次查看(过去 30 天)
Jorge Rodriguez
Jorge Rodriguez 2017-9-5
回答: KSSV 2017-9-6
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.

回答(1 个)

KSSV
KSSV 2017-9-6
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) ;

Community Treasure Hunt

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

Start Hunting!

Translated by