I need a code to count how many raw in my matrix above zero?
1 次查看(过去 30 天)
显示 更早的评论
I have matrix and I need a code to count how many row contain negative integer
B =
1 -1
2 -2
3 3
0 个评论
采纳的回答
Image Analyst
2019-11-17
Try using sum() and any():
B =[
1 -1
2 -2
3 3]
numberOfNegativeRows = sum(any(B < 0, 2))
更多回答(1 个)
Erivelton Gualter
2019-11-17
You might use the function find (https://www.mathworks.com/help/matlab/ref/find.html).
Check the following code:
B = [1,-1; 2,-2; 3,3];
length(find(B<0))
2 个评论
Image Analyst
2019-11-17
This counts the total number of negative numbers, not the number of rows. You might think what could happen if there are two negative numbers in one or more of the rows (answer in my Answer).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!