How can I set every value in a row to NaN based on two non-consecutive Quality columns?
2 次查看(过去 30 天)
显示 更早的评论
Hello, I am trying to throw out suspicious data from my .csv file based on two quality indicators. The file is (112,000 x 5) with the first column as datetime data, the second and fourth wind direction and wind speed respectively and the third and fifth their respective quality indicators (1 = good, 0 = bad).
I would like to set every value in a row (except the datetime) to NaN, if any one of the quality indicators is zero. How can that be done in the most efficient manner?
I tried to follow this community post (How do I delete an entire row if a specific column contains a zero? - MATLAB Answers - MATLAB Central (mathworks.com). I read the data in as wind with the function 'dataset', and then neither 'logical' nor the operator '==' worked for some reason.
wind = dataset('file','C:\my\directory\Example_data.csv', ...
'delimiter',',','format',['%s' repmat(' %f',1,4)]);
wind.Properties.VarNames = [{'Date'} {'Direction'} {'Quality1'} {'Velocity'} {'Quality2'}]; %new headers
wind((:,[3 5])==0) = NaN;
I work with Matlab R2018b.
2 个评论
Jiri Hajek
2023-1-11
Hi, note that datetime class oes not have NaN, there is a NaT instead. But there's also a glitch in the assignment
wind((:,[3 5])==0) = NaN;
which probably does not contain correct logical indices. Seems like you rather wanted to write this:
wind(wind(:,[3 5])==0) = NaN;
采纳的回答
Sajid Afaque
2023-1-11
编辑:Sajid Afaque
2023-1-11
first try to get the indices where qny of the quality indicator is 0 (i.e. find the row numbers where either column 3 or column 5 is 0)
then you can set columns of that row to NaN
PLEASE TRY THE CODE BELOW
quality1_index = find(wind.Kvalitet1 == 0);
quality2_index = find(wind.Kvalitet2 == 0);
final_indices = unique([quality1_index;quality2_index]);
wind.Kvalitet1(final_indices) = NaN;
wind.Kvalitet2(final_indices) = NaN;
line 1 and line 2 can be further optimized and written as below
final_indices = find(wind.Kvalitet1 == 0 |wind.Kvalitet2 == 0 ); %quality1_index & quality2_index are found together
3 个评论
Sajid Afaque
2023-1-11
编辑:Sajid Afaque
2023-1-11
nan_indices = find(isnan(wind.Kvalitet1) | isnan(wind.Kvalitet2)); %find in which rows column 3 or 5 has NaN
wind.Vindriktning(nan_indices) = NaN;
wind.Vindhastighet(nan_indices) = NaN;
Please dont forget to accept the answer, as it would help others in similar situtation as you
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!