如何从保持矩阵大小不变的矩阵中删除nan值 [英] how to remove nan values from a matrix keeping matrix size same

查看:197
本文介绍了如何从保持矩阵大小不变的矩阵中删除nan值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过一些计算获得了一个矩阵.

I obtained a matrix by doing some calculation.

th1 =

    0.3333    0.6667    0.2500       NaN
    0.5000         0       NaN    0.5000
    0.5000       NaN       NaN       NaN
    0.5000       NaN       NaN       NaN

我必须使用此矩阵进行进一步的计算,所以这些NaN是否会影响我的计算?如果是这样,那么我该如何删除这些以保持矩阵大小不变?

I have to use this matrix for further calculation, so will these NaN affect my calculation? If so, then how can I remove these keeping the matrix size same?

推荐答案

假设您要将矩阵中的值替换为有限的数字,可以使用

Assuming you want to replace the values in your matrix to some finite number, you can use isnan to help you do that.

具体来说,您可以执行以下操作:

Specifically, you can do something like this:

th1(isnan(th1)) = 0;

这会将所有NaN值替换为0.但是,如果您要删除 NaN值,则无法在MATLAB中使用数值矩阵来执行此操作,因为MATLAB不支持不均匀的矩阵.您唯一的选择是一个单元格数组,您可以做一个单元格数组,其中每个单元格都是矩阵中的一行.

This will replace all NaN values with 0. However, if you want to remove the NaN values, you can't do this with numeric matrices in MATLAB because MATLAB does not support uneven matrices. Your only option would be a cell array and what you can do is make a cell array where each cell is a row from your matrix.

具体来说,您可以执行以下操作:

Specifically, you can do something like this:

th1_out = arrayfun(@(x) th1(x,~isnan(th1(x,:))), 1:size(th1,1), 'uni', 0);

使用您的数据,我们得到以下信息:

Using your data, we get this:

>> celldisp(th1_out)

th1_out{1} =

    0.3333    0.6667    0.2500



th1_out{2} =

    0.5000         0    0.5000



th1_out{3} =

    0.5000



th1_out{4} =

    0.5000


但是,您并没有真正告诉我您打算执行的计算.根据要执行的计算,甚至根本不需要替换任何NaN值.您可以简单地忽略它们.例如,假设您要查找矩阵中每一行的平均值.您只需要计算每行NaN个值的总数,并在执行均值时将其考虑在内.


However, you haven't really told me what calculations you intend to perform. Depending on what calculations you want to perform, you don't even to replace any of the NaN values at all. You can simply ignore them. For example, let's say you wanted to find the average of every row in your matrix. You'd simply count up the total amount of NaN values per row and take that into account when performing your mean.

换句话说:

%// Create a temporary input matrix
th1_temp = th1;

%// Used to indicate what is NaN in your array
num_nan = isnan(th1);

%// Set the NaN values to zero in the temporary matrix
th1_temp(num_nan) = 0;

%// Calculate the average per row
average_th1 = sum(th1_temp, 2) ./ (size(th1,1) - sum(num_nan,2));

上面代码的逻辑是我们指示矩阵中的NaN是什么,然后,如果我们想查找每一行的平均值,则可以创建一个临时数据矩阵,将NaN的值设置为零,然后对每一行求和,然后除以元素的总数减去每行归为NaN 的那些元素.之所以要减去,是因为将NaN值设置为0,这在查找平均值时不会积聚任何东西,但是要除去这些0值在平均值中的贡献,您需要减去尽可能多的每行有一个值.

The logic of the above code is that we indicate what is NaN in your matrix, then if we wanted to find the average of each row, you would create a temporary data matrix that sets the NaN values to zero, then sum over each row and divide by the total number of elements minus those elements that were classified as NaN per row. You subtract by this much because by setting the NaN values to 0, this will not accumulate anything when finding the average, but in order to remove the contribution of these 0 values in your average, you need to subtract by as many NaN values there were per row.

在统计工具箱中,也可以使用 nanmean ,但是如果您无权使用此工具箱,则以上代码可移植.

In the Statistics Toolbox, the above code can also be replicated using nanmean but the above code is portable should you not have access to this toolbox.

如果要计算每列的平均值,则只需对最后一行代码稍作更改即可.

If you wanted to calculate the average per column, a slight change to the last line of code is all that is required:

%// Calculate the average per column
average_th1 = sum(th1_temp, 1) ./ (size(th1,2) - sum(num_nan,1));

基本上,我将1s更改为2s,将2s更改为1s.

Essentially, I changed the 1s to 2s and the 2s to 1s.

这篇关于如何从保持矩阵大小不变的矩阵中删除nan值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆