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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by