[英]Counting entries equal to zero in GNU Octave
GNU Octave中是否有任何命令允许我计算矩阵中的零(不计算非零)项?
有几种方法,下面将向您展示两种方法。
a = rand (5,5) > 0.5
a =
0 0 0 1 1
1 1 0 1 0
0 1 0 1 1
0 0 0 1 0
1 1 0 1 1
numel (find (a==0))
ans = 12
对于超大型矩阵,此速度更快(请参见下文)
numel (a) - nnz (a)
ans = 12
大型矩阵的速度测试:
a = rand (1e6, 1e6) > 0.5;
tic
numel (find (a==0))
toc
tic
numel (a) - nnz (a)
toc
这使
ans = 499566
Elapsed time is 0.060837 seconds.
ans = 499566
Elapsed time is 0.0187149 seconds.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.