如何在MATLAB中摆脱NaN?

时间:2021-12-15 23:50:51

I have files which have many empty cells which appear as NaNs when I use cell2mat, but the problem is when I need to get the average values I cannot work with this as it shows error with NaN. In excel it overlooks NaN values, so how do I do the same in MATLAB?

我有很多空单元格的文件,当我使用cell2mat时,它们显示为NaNs,但问题是当我需要获得平均值时,我无法使用它,因为它显示NaN的错误。在excel中它忽略了NaN值,那么我如何在MATLAB中做同样的事情呢?

In addition, I am writing a file using xlswrite:

另外,我正在使用xlswrite编写一个文件:

xlswrite('test.xls',M);

I have data in all rows except 1. How do I write:

我的所有行都有数据,除了1.我怎么写:

M(1,:) = ('time', 'count', 'length', 'width')

In other words, I want M(1,1)='time', M(1,2)='count', and so on. I have data from M(2,1) to M(10,20). How can I do this?

换句话说,我想要M(1,1)='时间',M(1,2)='计数',依此类推。我有从M(2,1)到M(10,20)的数据。我怎样才能做到这一点?

5 个解决方案

#1


7  

Use ' isfinite ' function to get rid of all NaN and infinities

使用'isfinite'函数来消除所有NaN和无穷大

A=A(isfinite(A))

A = A(ISFINITE(A))

%create the cell array containing the column headers columnHeader = {'Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5',' '};

%创建包含列标题的单元格数组columnHeader = {'Column 1','Column 2','Column 3','Column 4','Column 5','}};

%write the column headers first xlswrite('myFile1.xls', columnHeader );

%首先写下列标题xlswrite('myFile1.xls',columnHeader);

% write the data directly underneath the column headers xlswrite('newFile.xls',M,'Sheet1','A2');

%直接在列标题xlswrite('newFile.xls',M,'Sheet1','A2')下面写入数据;

#2


9  

As AP correctly points out, you can use the function isfinite to find and keep only finite values in your matrix. You can also use the function isnan. However, removing values from your matrix can have the unintended consequence of reshaping your matrix into a row or column vector:

正如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

Another alternative is to use some functions from the Statistics Toolbox (if you have access to it) that are designed to deal with matrices containing NaN values. Since you mention taking averages, you may want to check out nanmean:

另一种方法是使用统计工具箱中的一些函数(如果您有权访问它们),这些函数用于处理包含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



EDIT: To answer your additional question on the use of xlswrite, this sample code should illustrate one way you can write your data:

编辑:要回答有关使用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

#3


5  

Statistics Toolbox has several statistical functions to deal with NaN values. See nanmean, nanmedian, nanstd, nanmin, nanmax, etc.

Statistics Toolbox有几个统计函数来处理NaN值。参见nanmean,nanmedian,nanstd,nanmin,nanmax等。

#4


0  

You can set NaN's to an arbitrary number like so:

您可以将NaN设置为任意数字,如下所示:

mat(isnan(mat))=7 // my lucky number of choice. 

#5


0  

May be too late, but...

可能为时已晚,但......

x = [1 2 3; 4 inf 6; 7 -inf NaN];
x(find(x == inf)) = 0; //for inf
x(find(x == -inf)) = 0; //for -inf
x(find(isnan(x))) = 0; //for NaN

#1


7  

Use ' isfinite ' function to get rid of all NaN and infinities

使用'isfinite'函数来消除所有NaN和无穷大

A=A(isfinite(A))

A = A(ISFINITE(A))

%create the cell array containing the column headers columnHeader = {'Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5',' '};

%创建包含列标题的单元格数组columnHeader = {'Column 1','Column 2','Column 3','Column 4','Column 5','}};

%write the column headers first xlswrite('myFile1.xls', columnHeader );

%首先写下列标题xlswrite('myFile1.xls',columnHeader);

% write the data directly underneath the column headers xlswrite('newFile.xls',M,'Sheet1','A2');

%直接在列标题xlswrite('newFile.xls',M,'Sheet1','A2')下面写入数据;

#2


9  

As AP correctly points out, you can use the function isfinite to find and keep only finite values in your matrix. You can also use the function isnan. However, removing values from your matrix can have the unintended consequence of reshaping your matrix into a row or column vector:

正如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

Another alternative is to use some functions from the Statistics Toolbox (if you have access to it) that are designed to deal with matrices containing NaN values. Since you mention taking averages, you may want to check out nanmean:

另一种方法是使用统计工具箱中的一些函数(如果您有权访问它们),这些函数用于处理包含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



EDIT: To answer your additional question on the use of xlswrite, this sample code should illustrate one way you can write your data:

编辑:要回答有关使用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

#3


5  

Statistics Toolbox has several statistical functions to deal with NaN values. See nanmean, nanmedian, nanstd, nanmin, nanmax, etc.

Statistics Toolbox有几个统计函数来处理NaN值。参见nanmean,nanmedian,nanstd,nanmin,nanmax等。

#4


0  

You can set NaN's to an arbitrary number like so:

您可以将NaN设置为任意数字,如下所示:

mat(isnan(mat))=7 // my lucky number of choice. 

#5


0  

May be too late, but...

可能为时已晚,但......

x = [1 2 3; 4 inf 6; 7 -inf NaN];
x(find(x == inf)) = 0; //for inf
x(find(x == -inf)) = 0; //for -inf
x(find(isnan(x))) = 0; //for NaN