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
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;
Nils
Nils 2023 年 1 月 11 日
Yes, but I had also already tried this and I get the Error: "Undefined operator '==' for input arguments of type 'dataset'."

サインインしてコメントする。

採用された回答

Sajid Afaque
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
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
Nils
Nils 2023 年 1 月 11 日
Works perfectly, you made my day!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by