从数组中删除NaN值并向左移动 [英] Removing NaN Values from Array and shift left

查看:44
本文介绍了从数组中删除NaN值并向左移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有NaN值的34 x 1096数组.

I have a 34 x 1096 array that has NaN values.

 A = 
 NaN    0.2500     NaN    0.3750       NaN
 NaN    0.1100     NaN    0.4310     0.1250
 NaN    0.1250    0.2500  0.3750     0.4310

我想要

 A = 
0.2500   0.3750       NaN     NaN       NaN
0.1100   0.4310     0.1250    NaN       NaN
0.1250   0.2500     0.3750   0.4310     NaN     

什么是简单的方法?

推荐答案

一种简单的方法是在每行上使用带有~isnan的for循环,例如:

a simple way will be to use a for loop with ~isnan on each row, for example:

B=NaN(size(A));
for n=1:size(A,1)
    B(n,1:sum(~isnan(A(n,:))))=A(n,~isnan(A(n,:)));
end    

B =
0.2500    0.3750       NaN       NaN       NaN
0.1100    0.4310    0.1250       NaN       NaN
0.1250    0.2500    0.3750    0.4310       NaN

然后您可以根据需要分配A=B ...是的,这可以在没有for循环的情况下完成,

you can then assign A=B if you must... and yes this can be done without a for loop, but why bother in this case?

这篇关于从数组中删除NaN值并向左移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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