本文介绍了MATLAB:如何有效地从矩阵中删除 NaN 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来有效地从 MATLAB 中的矩阵中删除 NaN 数(即不使用 for 循环)

I am looking for a way to remove the NaN numbers from a matrix in MATLAB efficiently (i.e. without using a for loop)

我将提供一个简单的例子来说明我想要实现的目标:

I will provide a quick example to illustrate what I am trying to achieve:

假设我有一个矩阵 M:

Say I have a matrix M:

          3.00          1.00
          1.00          3.00
           NaN           NaN
          3.00          3.00
          1.00          1.00
           NaN           NaN
           NaN           NaN
           NaN           NaN
           NaN           NaN
           NaN           NaN

我想找到一种方法将其更改为

I would like to find a way to change this to

          3.00          1.00
          1.00          3.00
          3.00          3.00
          1.00          1.00

我目前正在尝试通过 M(isfinite(M)) 执行此操作,但最终返回的是向量而不是矩阵.有什么技巧可以让它返回一个矩阵吗?

I am currently trying to do this via M(isfinite(M)) but that ends up returning a vector instead of the matrix. Is there a trick to have it return a matrix instead?

推荐答案

如果每行没有 NaN 或所有 NaN,您可以使用以下方法进行删除:

If you have either no NaNs or all NaNs on each row, you can do the removal using:

M(isfinite(M(:, 1)), :)

这篇关于MATLAB:如何有效地从矩阵中删除 NaN 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 17:29