I need a code to count how many raw in my matrix above zero?
조회 수: 3 (최근 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에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!