使用MATLAB从Matrix中找到正值的数量 [英] Find the number of positive values from Matrix using MATLAB

查看:59
本文介绍了使用MATLAB从Matrix中找到正值的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个6x6双矩阵A:

 1   1   2  -1  -2   2

-1  -3   1   1   2   1

 3   5   1  -1  -3   3

 4  -5   2   2   1  -3

-4   1   3   3  -2   3

 2   3  -3  -4   2  -3

如何使用MATLAB从该矩阵中找到正值的数量?

How do I find the number of positive values from this matrix using MATLAB?

推荐答案

您可以使用

sum(A(:) >= 0)
ans = 23

出于好奇,对性能进行了快速检查:

Out of curiosity a quick performance check:

A = randn(10000);

tic 
sum(A(:) >= 0);
toc

tic 
nnz(sign(A)+1);
toc

tic 
size(find(A>=0),1);
toc

tic 
length(A(A>=0));
toc

Elapsed time is 0.147514 seconds.
Elapsed time is 0.769115 seconds.
Elapsed time is 1.107935 seconds.
Elapsed time is 0.820353 seconds.

这篇关于使用MATLAB从Matrix中找到正值的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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