将 NaN 替换为上面的值(用于匹配 ID)- Matlab
我有一个包含 Col1 ID 和 Col2 值的矩阵。
mat = [ ...
1000 3
1000 4
1000 nan
1000 nan
1000 5
2222 1
2222 2
2222 nan
3333 nan
4444 1 ] ;
我需要将 nan
替换为其上方行中的值,但须遵守以下条件:上方行应具有相同的 ID。
回答:
mat = [ ...
1000 3
1000 4
1000 4
1000 4
1000 5
2222 1
2222 2
2222 2
3333 nan
4444 1 ] ;
您能建议一种矢量化方法吗?
I have a matrix with Col1 IDs and Col2 Values.
mat = [ ...
1000 3
1000 4
1000 nan
1000 nan
1000 5
2222 1
2222 2
2222 nan
3333 nan
4444 1 ] ;
I need to replace nan
with the value in the row above it, but subject to the condition: row above should have the same ID.
Answer:
mat = [ ...
1000 3
1000 4
1000 4
1000 4
1000 5
2222 1
2222 2
2222 2
3333 nan
4444 1 ] ;
Can you suggest a vectorized approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此代码不会处理具有相同 ID 的连续 NaN。
对示例数据运行两次即可完成任务。
This code won't handle consecutive NaNs with the same ID.
Running it twice on the sample data completes the task.