正如 AP 正确指出的那样,您可以使用该函数isfinite
在矩阵中查找并仅保留有限值。您也可以使用该功能isnan
。但是,从矩阵中删除值可能会导致将矩阵重新整形为行或列向量的意外后果:
>> mat = [1 2 3; 4 NaN 6; 7 8 9] % A sample 3-by-3 matrix
mat =
1 2 3
4 NaN 6
7 8 9
>> mat = mat(~isnan(mat)) % Removing the NaN gives you an 8-by-1 vector
mat =
1
4
7
2
8
3
6
9
另一种选择是使用统计工具箱中的一些函数(如果您可以访问它),这些函数旨在处理包含 NaN 值的矩阵。由于您提到取平均值,您可能需要查看nanmean
:
>> mat = [1 2 3; 4 NaN 6; 7 8 9];
>> nanmean(mat)
ans =
4 5 6 % The column means computed by ignoring NaN values
编辑:要回答有关使用的其他问题xlswrite
,此示例代码应说明您可以编写数据的一种方式:
C = {'time','count','length','width'}; % A cell array of strings
M = rand(10,20); % A 10-by-20 array of random values
xlswrite('test.xls',C); % Writes C to cells A1 through D1
xlswrite('test.xls',M,'A2:T11'); % Writes M to cells A2 through T11