0

我想删除所有超过特定数量的 NaN 的列(比如说 5 个 NaN)。有没有没有循环的方法?

4

2 回答 2

2

不需要循环:

matFixed = MAT( :, sum( isnan( MAT ) ) <= 5);

一步步:

nanMat = isnan(MAT); % convert MAT to logical with 1 indicating value is a NAN
nNanInCol = sum(nanMat); %compute the number of NANs in each col
colIdx = nNanInCol <= 5; % determine which columns are "valid"
matFixed = MAT(:, colIdx); % create new matrix using only the good columns of MAT
于 2013-03-02T18:38:18.560 回答
0

这很容易:

%# define a matrix with NaNs
A = rand(10); %# 10x10 array
A(A<0.5) = NaN; %# each column contains on average 5 NaNs

%# count NaNs
nanCt = sum(isnan(A),1);

%# remove columns with too many NaNs
A(:,nanCt > 5) = [];
于 2013-03-02T18:38:08.873 回答