Excluding rows of data with NaN and writing to a new matrix, using indexing
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Jos (10584)
on 2 Oct 2013
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),:)
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!