I need a code to count how many raw in my matrix above zero?

조회 수: 3 (최근 30일)
Abdullah Alsuhaymi
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

채택된 답변

Image Analyst
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
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
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).
Abdullah Alsuhaymi
Abdullah Alsuhaymi 2019년 11월 17일
yes , this count the total number of nagative numbers in the matrix but I am looking for number of nagative rows ?
can you help me please ?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by