MATLAB 仅从矩阵中删除某些零
我已经看到很多关于如何删除前导和/或尾随零以及如何从向量或矩阵中删除所有零的答案。不过,我需要做的只是删除其中一些。我有两个矩阵,我只想删除它们都为零的条目。它们是二维 x 和 y 坐标,使用特征求解(如果需要,我可以提供更多详细信息),我只想删除两个矩阵在相同索引处包含零的值。我可以轻松地将矩阵转换为向量并使用向量,因此在任何一种情况下的任何帮助将不胜感激。
I have seen plenty of answers regarding how to remove leading and/or trailing zeros, and how to remove all zeros from a vector or matrix. What I need to do, though, is only remove some of them. I have two matrices, and I only want to remove the entries where both of them are zero. They are two-dimensional x and y coordinates, solved using characteristics (I can give more detail if needed) and I just want to remove the values where both matrices contain zeros at the same indices. I can easily convert the matrices into vectors and work with vectors, so any help in either case would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了简单起见,我们假设您使用名为
X
和Y
的向量(长度相同),并且您只想删除两个向量都为的条目零。方法如下(未测试):编辑:正如奥利在下面指出的那样(以及斯特凡诺进一步解释),您应该使用逻辑索引以获得更好的性能。
For the sake of simplicity, let's assume you're using vectors called
X
andY
(of the same length), and you want to remove only those entries where both vectors are zero. Here's how (not tested):Edit: As Oli has pointed out below (and stefano explained further), you should use logical indexing for better performance.