I need a code to count how many raw in my matrix above zero?
1 回表示 (過去 30 日間)
古いコメントを表示
Abdullah Alsuhaymi
2019 年 11 月 17 日
コメント済み: Abdullah Alsuhaymi
2019 年 11 月 17 日
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!