如何找到矩阵中非零行的索引? [英] How to find the indices of nonzero rows in a matrix?

查看:246
本文介绍了如何找到矩阵中非零行的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查找矩阵中非零行的索引?

How to find the indices of nonzero rows in a matrix?

示例:

A = [
       14  0  6  9  8  17
       85 14  1  3  0  99
       0   0  0  0  0   0 
       29  4  5  8  7  46
       0   0  0  0  0   0
       17  0  5  0  0  49
]

所需结果:

   V =[1 2 4 6]

推荐答案

您可以使用

   ix = any(x,2);

any检查是否存在任何不为零的元素.第二个参数代表每行"计算.

any check whether there is any element that is not a zero. The second argument stands for "per-row" computation.

如果要获取数字索引,可以使用find函数:

If you want to get the numeric index, you can use find function:

   numIx = find(ix);


另一种方法:


Another method:

  ix = sum(abs(x),2)~=0;

这篇关于如何找到矩阵中非零行的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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