How can I set every value in a row to NaN based on two non-consecutive Quality columns?

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Jiri Hajek
Jiri Hajek il 11 Gen 2023
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 il 11 Gen 2023
Yes, but I had also already tried this and I get the Error: "Undefined operator '==' for input arguments of type 'dataset'."

Accedi per commentare.

Risposta accettata

Sajid Afaque
Sajid Afaque il 11 Gen 2023
Modificato: Sajid Afaque il 11 Gen 2023
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 Commenti
Sajid Afaque
Sajid Afaque il 11 Gen 2023
Modificato: Sajid Afaque il 11 Gen 2023
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

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by