Excluding rows of data with NaN and writing to a new matrix, using indexing
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a data matix of 84761 x 8 (an example is attached: EditedWindTempMatrix_eg.txt).
I would like to remove the rows that have any NaNs in and re-write the rows with no NaNs to a new matrix (QCMat).
I have used 'isfinite' to determine where there is data. This returns a 84761 x 8 logical matrix (an example is attached: idx_eg.txt)
When I run the script, I receive an error (Subscripted assignment dimension mismatch.) associated with line:
QCMat(Rowassign,:)=EditedWindTempMatrix(i,:);
How do I assign the rows of data to the new matrix?
Thank you, Jenny
%%remove the NaNs from the data and write out the data to a new matrix QCMat
idx = isfinite(EditedWindTempMatrix); % returns a matrix with 8 columns with 1 = true; 0 = false
% Check each row of data to see whether it has data - using isfinite - and write the rows with data to a new matrix
[NrRows, NrCols]=size(EditedWindTempMatrix);
Rowassign=1;
for i = 1:NrRows;
Datacheck=sum(idx(i,:)); % add the results along a row from the isfinte command. If the ans <NrCols then there is a NaN in that row so we leave it out of the new matrix.
if Datacheck==NrCols;
QCMat(Rowassign,:)=EditedWindTempMatrix(i,:);
Rowassign=Rowassign+1;
end
end
댓글 수: 0
채택된 답변
Jos (10584)
2013년 10월 2일
A = [ 1 2 3 ; 4 5 NaN ; 6 7 8 ; NaN NaN 9 ; 10 NaN 11 ; 12 13 14] ; % a matrix with NaNs in some rows
tf1 = isnan(A) % where are the NaNs
tf2 = any(tf1,2) % where are the rows with NaN
tf3 = ~tf2 % where are the rows with no NaNs
B = A(tf3,:) % select those rows
% or use all (with a little different approach and in one big step)
C = A(all(~isnan(A),2),:)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!